forum

Home / DeveloperSection / Forums / Copy data from two separate textboxes to one txt file

Copy data from two separate textboxes to one txt file

Anonymous User198503-Nov-2014

I am searching all the exe,dll or both files in a particular folder. I wish to copy the data in the same way in txt file as it is displayed in the textboxes like (FileName| Path.....) pair value.File.WriteAllLines(@"C:\BigB.txt",files.ToArray()); will copy only the path in BigB.txt but I can't get them in the way shown below

File1.exe Path1File2.exe Path2......

since I can't use something like File.WriteAllLines(@"C:\BigB.txt,Path.GetFileName(files) + files.ToArray())"

Though I have an idea of copying all the filenames in another list, like this

List<String> filename =newList<String>(); filename.AddRange(files);

and then copying the data into txt file with the help of both the lists. But then again how to go through each entry one by one ? :|

XAML Code

<Windowx:Class="FileFinder.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="FileCopier"
    WindowStartupLocation="CenterScreen"Width="1366"Height="758">
<GridBackground="LavenderBlush">
 
    <LabelHeight="40"Margin="240,0,234,0"Name="label1"VerticalAlignment="Top"FontSize="28"HorizontalContentAlignment="Center"Foreground="DarkSeaGreen"FontWeight="Bold"FontFamily="Broadway">Welcome To</Label>
 
    <LabelHeight="55"Margin="194,35,194,0"Name="label2"VerticalAlignment="Top"HorizontalContentAlignment="Center"FontWeight="Bold"FontSize="45"Foreground="Chocolate"FontFamily="Colonna MT">FILE FINDER</Label>
 
    <TextBoxIsReadOnly="True"AutoWordSelection="True"VerticalScrollBarVisibility="Auto"Margin="444.419,211,20,21"Name="textbox1"Background="Pink"Opacity="0.5"ScrollBar.Scroll="Scroll"BorderThickness="0"FontSize="16"TextWrapping="Wrap"></TextBox>
 
    <ButtonToolTip="Click to search exe files"Height="45"HorizontalAlignment="Left"Margin="366,96,0,0"Name="button1"VerticalAlignment="Top"Width="112"Background="LavenderBlush"BorderThickness="1"BorderBrush="DarkSeaGreen"FontSize="24"FontWeight="Bold"Foreground="DarkSeaGreen"Click="button1_Click"FontFamily="Broadway"Cursor="Hand">.exe</Button>
 
    <ButtonToolTip="Click to search dll files" Margin="602,96,619,0"Name="button2"Background="LavenderBlush"BorderBrush="DarkSeaGreen"Foreground="DarkSeaGreen"FontSize="24"FontWeight="Bold"Click="button2_Click"FontFamily="Broadway"Cursor="Hand"Height="45"VerticalAlignment="Top">.dll</Button>
 
    <ButtonBackground="LavenderBlush"BorderBrush="DarkSeaGreen"FontSize="24"FontWeight="Bold"Foreground="DarkSeaGreen"Height="45"HorizontalAlignment="Right"Margin="0,96,383,0"Name="button3"VerticalAlignment="Top"Width="122"Click="button3_Click"FontFamily="Broadway">All</Button>
 
    <LabelFontFamily="Broadway"FontSize="28"FontWeight="Bold"Foreground="Black"Height="40"HorizontalContentAlignment="Center"Margin="12,165,0,0"Name="label3"VerticalAlignment="Top"HorizontalAlignment="Left"Width="320"Opacity="0.6">Filename</Label>
 
    <LabelFontFamily="Broadway"FontSize="28"FontWeight="Bold"Foreground="Black"Height="40"HorizontalContentAlignment="Center"Margin="0,165,234,0"Name="label4"VerticalAlignment="Top"Opacity="0.6"HorizontalAlignment="Right"Width="86.627">Path</Label>
 
    <Button  Name="button4"Click="button4_Click"Width="32"Height="32"HorizontalAlignment="Right"Margin="0,173,175,0"VerticalAlignment="Top"BorderThickness="0"Background="LavenderBlush"ToolTip="PageUp">
        <StackPanel>
            <ImageSource="C:\Users\Vipul.Sharma\Documents\Visual Studio 2008\Projects\FileFinder\FileFinder\PageUp.png"></Image>
        </StackPanel>
    </Button>
 
    <ButtonName="button5"Click="button5_Click"Width="32"Height="32"HorizontalAlignment="Right"Margin="0,173,140,0"VerticalAlignment="Top"BorderThickness="0"Background="LavenderBlush"ToolTip="PageUp">
        <StackPanel>
            <ImageSource="C:\Users\Vipul.Sharma\Documents\Visual Studio 2008\Projects\FileFinder\FileFinder\PageDown.png"Opacity="0.7"></Image>
        </StackPanel>
    </Button>
 
    <TextBoxIsReadOnly="True"AutoWordSelection="True"VerticalScrollBarVisibility="Visible"Margin="20,211,0,21"Name="textbox2"Background="Pink"Opacity="0.5"ScrollBar.Scroll="Scroll"BorderThickness="0"FontSize="16"TextWrapping="Wrap"HorizontalAlignment="Left"Width="418.697"></TextBox>   
</Grid>

C# Code

privatevoid button1_Click(object sender, RoutedEventArgs e)
        {
            List<String> files = newList<String>();
            String[] extensions = newString[] { "*.exe" };
 
            foreach (String extension in extensions)
            {
                String[] lol = Directory.GetFiles(@"C:\\", "*.exe", SearchOption.AllDirectories);
 
                foreach (String file in lol)
                    files.Add(file);
            }
 
            textbox1.Clear();
 
            for (int i = 0; i < files.Count; i++)
            {
                textbox2.Text += Path.GetFileName(files[i]) + "\n";
                textbox1.Text += files[i] + "\n";
            }
        }
 
        privatevoid button2_Click(object sender, RoutedEventArgs e)
        {
            List<String> files = newList<String>();
            String[] extensions = new[] { "*.dll" };
 
            foreach (String extension in extensions)
            {
                String[] lol = Directory.GetFiles(@"C:\\", "*.dll", SearchOption.AllDirectories);
 
                foreach (String file in lol)
                    files.Add(file);
            }
            textbox1.Clear();
            textbox1.Clear();
            for (int i = 0; i < files.Count; i++)
            {
                textbox2.Text += Path.GetFileName(files[i]) + "\n";
                textbox1.Text += files[i] + "\n";
            }
        }
 
        privatevoid button3_Click(object sender, RoutedEventArgs e)
        {
            List<String> files = newList<String>();
            String[] extensions = new[] { "*.dll", "*.exe" };
 
            foreach (String extension in extensions)
            {
                String[] lol = Directory.GetFiles(@"C:\\", "*.exe", SearchOption.AllDirectories);
 
                foreach (String file in lol)
                    files.Add(file);
            }
            textbox1.Clear();
            textbox1.Clear();
            for (int i = 0; i < files.Count; i++)
            {
                textbox2.Text += Path.GetFileName(files[i]) + "\n";
                textbox1.Text += files[i] + "\n";
 
            }
 
            foreach (String extension in extensions)
            {
                String[] lol = Directory.GetFiles(@"C:\\", "*.dll", SearchOption.AllDirectories);
 
                foreach (String file in lol)
                    files.Add(file);
            }
            for (int i = 0; i < files.Count; i++)
            {
                textbox2.Text += Path.GetFileName(files[i]) + "\n";
                textbox1.Text += files[i] + "\n";
            }
        }
 
 
        privatevoid button4_Click(object sender, RoutedEventArgs e)
        {
            textbox2.PageUp();
            textbox1.PageUp();
        }
 
        privatevoid button5_Click(object sender, RoutedEventArgs e)
        {
            textbox1.PageDown();
            textbox2.PageDown();
        }
 
        //To synchronize scrollbar of textbox1 and textbox2
        privatevoid Scroll(object sender, ScrollEventArgs e)
        {
            if (sender == textbox1)
            {
                textbox1.ScrollToVerticalOffset(e.NewValue);
            }
            else
            {
                textbox1.ScrollToVerticalOffset(e.NewValue);
            }
        }


Updated on 04-Nov-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By