将数据从两个单独的文本框复制到一个文本文件

时间:2014-10-29 08:24:41

标签: c# wpf xaml .net-3.5

我正在搜索特定文件夹中的所有exe,dll或两个文件。 我希望以相同的方式在txt文件中复制数据,因为它在文本框中显示,如同 (FileName | Path .....)对值。 File.WriteAllLines(@“C:\ BigB.txt”,files.ToArray()); 只会复制BigB.txt中的路径,但我无法按照下面显示的方式获取它们/ p>

File1.exe                Path1
File2.exe                Path2
    .                      . 
    .                      . 
    .                      . 

因为我不能使用 File.WriteAllLines(@“C:\ BigB.txt,Path.GetFileName(files)+ files.ToArray())”

虽然我想知道将所有文件名复制到另一个列表中,例如

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

然后在两个列表的帮助下将数据复制到txt文件中。但是又如何逐个浏览每个条目? :|

XAML代码

<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#代码

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);
        }
    }

1 个答案:

答案 0 :(得分:0)

使用Path.GetFullPath(files[i]获取特定文件的路径。 然后将其与Path.GetFileName一起打印出来。

修改您的代码,如下所示

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);

尝试遵循逻辑。因为编码是以非ide模式键入的