Home > DeveloperSection > Forums > Copy data from two separate textboxes to one txt file
Goti Bandu

Total Post:119

Points:835
Posted on    November-03-2014 11:37 PM

 .NET C#  WPF  XAML 
Ratings:


 1 Reply(s)
 575  View(s)
Rate this:

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                Path1
File2.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 = new List<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

<Window x: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">

<Grid Background="LavenderBlush">

 

    <Label Height="40" Margin="240,0,234,0" Name="label1" VerticalAlignment="Top" FontSize="28" HorizontalContentAlignment="Center" Foreground="DarkSeaGreen" FontWeight="Bold" FontFamily="Broadway">Welcome To</Label>

 

    <Label Height="55" Margin="194,35,194,0" Name="label2" VerticalAlignment="Top" HorizontalContentAlignment="Center" FontWeight="Bold" FontSize="45" Foreground="Chocolate" FontFamily="Colonna MT">FILE FINDER</Label>

 

    <TextBox IsReadOnly="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>

 

    <Button ToolTip="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>

 

    <Button ToolTip="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>

 

    <Button Background="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>

 

    <Label FontFamily="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>

 

    <Label FontFamily="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>

            <Image Source="C:\Users\Vipul.Sharma\Documents\Visual Studio 2008\Projects\FileFinder\FileFinder\PageUp.png"></Image>

        </StackPanel>

    </Button>

 

    <Button Name="button5" Click="button5_Click" Width="32" Height="32" HorizontalAlignment="Right" Margin="0,173,140,0" VerticalAlignment="Top" BorderThickness="0" Background="LavenderBlush" ToolTip="PageUp">

        <StackPanel>

            <Image Source="C:\Users\Vipul.Sharma\Documents\Visual Studio 2008\Projects\FileFinder\FileFinder\PageDown.png" Opacity="0.7"></Image>

        </StackPanel>

    </Button>

 

    <TextBox IsReadOnly="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

private void button1_Click(object sender, RoutedEventArgs e)

        {

            List<String> files = new List<String>();

            String[] extensions = new String[] { "*.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";

            }

        }

 

        private void button2_Click(object sender, RoutedEventArgs e)

        {

            List<String> files = new List<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";

            }

        }

 

        private void button3_Click(object sender, RoutedEventArgs e)

        {

            List<String> files = new List<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";

            }

        }

 

 

        private void button4_Click(object sender, RoutedEventArgs e)

        {

            textbox2.PageUp();

            textbox1.PageUp();

        }

 

        private void button5_Click(object sender, RoutedEventArgs e)

        {

            textbox1.PageDown();

            textbox2.PageDown();

        }

 

        //To synchronize scrollbar of textbox1 and textbox2

        private void Scroll(object sender, ScrollEventArgs e)

        {

            if (sender == textbox1)

            {

                textbox1.ScrollToVerticalOffset(e.NewValue);

            }

            else

            {

                textbox1.ScrollToVerticalOffset(e.NewValue);

            }

        }



Tanuj Kumar

Total Post:134

Points:940
Posted on    November-04-2014 2:35 AM

use Path.GetFullPath(files[i] to get the path for the particular file. then print this along with the Path.GetFileName.

Modify your code like following

List<string> lstResult = new List<string>();

for (int i = 0; i < files.Length; i++)

{

      lstResult.Add(Path.GetFileName(files[i]) + "           " + Path.GetFullPath(files[i]));

}

Files.WriteAllLines(lst);

try to follow the logic. since coding was typed in a non-ide mode


Don't want to miss updates? Please click the below button!

Follow MindStick