简体   繁体   English

Windows窗体中的控制台输出

[英]Console output in Windows Form

I'm building C# Windows Form application to search a specific directory. 我正在构建C#Windows窗体应用程序以搜索特定目录。 Output of the paths searched are sent to a console. 搜索到的路径的输出将发送到控制台。 How can I direct that method into a Windows' form? 如何将该方法导入Windows窗体?

        // Display the pathof each examined file.   
        foreach (var DisplayPath in fileList)
        {    
           Console.WriteLine(DisplayPath);
        } 

The simplest approach is to add a TextBox control to your form and set it to be multiline. 最简单的方法是将一个TextBox控件添加到您的窗体并将其设置为多行。 Then each line of output is just appended to the control like this... 然后将输出的每一行都像这样附加到控件中...

textBox1.Text += DisplayPath;

...if you prefer each line to be inserted at the top, making it easier to see the output without have to constantly scroll to the bottom of the TextBox then do this instead... ...如果您希望将每一行都插入到顶部,可以更轻松地查看输出而不必不断滚动到TextBox的底部,然后执行此操作...

textBox1.Text = DisplayPath + textBox1.Textl

Thanks Phil. 谢谢菲尔。 Problem I see now, I am storing the whole list of paths into a variable. 我现在看到的问题是,我将整个路径列表存储到一个变量中。 The output is a block of paths. 输出是路径块。 I like to have each path going to 'textBox1' dynamically changing, where every time a path is read, 'textBox1' displays it. 我希望每个路径都将动态更改为“ textBox1”,每次读取路径时,“ textBox1”都会显示该路径。 Here is what I have: 这是我所拥有的:

        // Get IEnumerable (as in a list) on all files by recursively scanning directory.
        var fileList = Directory.EnumerateFiles(AppDirectory, "*", SearchOption.AllDirectories);           

        // Retrieve the size of files.
        long fileSize = (from file in fileList let fileInfo = new FileInfo(file) select fileInfo.Length).Sum();

        // Display the path and file of each examined file.       
        foreach (var DisplayPath in fileList)
        {                
            textBox2.Text += DisplayPath;
        }         

I am not sure if I understand your requirements correctly. 我不确定我是否正确理解您的要求。 You can try this and tell us if this or something similar is what you are searching: 您可以尝试一下,并告诉我们您搜索的是这个还是类似的东西:

(just quick and dirty hacked together as a starting point): (只是以快速和肮脏的技巧作为起点):

using System.Threading.Tasks;
....

Task.Factory.StartNew(() =>
{
    // Get IEnumerable (as in a list) on all files by recursively scanning directory.
    var fileList = Directory.EnumerateFiles(AppDirectory, "*", SearchOption.AllDirectories);

    long totalSize = 0;

    // Retrieve the size of files.
    foreach (string path in fileList)
    {
        FileInfo fileInfo = new FileInfo(path);
        long size = fileInfo.Length;
        totalSize += size;

        Invoke((Action)(()  =>
        {
            listBox1.Items.Add(path + ": " + size.ToString());
            textBox1.Text = totalSize.ToString();

        }));

    }

});

This uses a ListBox for the display of the path and the size and a TextBox for display of the total size. 这将使用ListBox来显示路径和大小,并使用TextBox来显示总大小。

The Invoke call is important, as it allows to access the GUI from a thread (and a Task is another thread). Invoke调用很重要,因为它允许从线程(而Task是另一个线程)访问GUI。 If you omit the Invoke call, you get a cross thread exception. 如果省略Invoke调用,则会得到一个跨线程异常。

On Stackoverflow you find many informations about this issue (accessing GUI from background thread and many different (better) ways to handle this. 在Stackoverflow上,您会找到有关此问题的许多信息(从后台线程访问GUI以及许多其他(更好)的方式来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM