【问题标题】:Copy data from two separate textboxes to one txt file将数据从两个单独的文本框复制到一个 txt 文件
【发布时间】:2014-12-24 21:07:23
【问题描述】:

我正在搜索特定文件夹中的所有 exe、dll 或两个文件。 我希望 以与文本框中显示的方式相同的方式复制 txt 文件中的数据,例如 (文件名|路径.....)对值。 File.WriteAllLines(@"C:\BigB.txt",files.ToArray()); 将仅复制 BigB.txt 中的路径,但我无法通过以下方式获取它们

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

【问题讨论】:

    标签: c# wpf xaml .net-3.5


    【解决方案1】:

    使用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模式下输入的

    【讨论】:

    • @Mohamed for (int i = 0; i
    • 非常感谢@Mohamed!只需要一点点修改就可以了。 List&lt;string&gt; lstResult = new List&lt;string&gt;(); for (int i = 0; i &lt; files.Count; i++) { lstResult.Add(Path.GetFileName(files[i]) + " " + Path.GetFullPath(files[i])); } File.WriteAllLines(@"C:\BigB.txt",lstResult.ToArray());
    猜你喜欢
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-05
    相关资源
    最近更新 更多