How to write a Text file from this Output

送分小仙女□ 提交于 2019-12-06 16:06:12

If I am understanding it correctly, use a simple I/O operation.

   using(StreamWriter writer = new StreamWriter("debug.txt", true))
    {
        foreach (string item in lstFilesFound.Items)
        {
            writer.WriteLine(item.ToString());
        }
    }

Few extra pointers:

In DirSearch, create a variable for Directory.GetDirectories(sDir). Your present code is causing this thing to calculate in every loop. Look for some more similar refactoring code in other areas.

var dirs = Directory.GetDirectories(sDir);
foreach (string d in dirs)
{
    var files = Directory.GetFiles(d, txtFile.Text);
    foreach (string f in files)
    {
        lstFilesFound.Items.Add(f);
    }
    DirSearch(d);
}

Hope it helps.

I'm assuming it's the list of found files you're wanting to save to a text file, how about this on your save event (rough code so not extensively tested)?

using (FileStream fs = new FileStream("c:\\files.txt", FileMode.Create, FileAccess.Write))
{
    using (StreamWriter sw = new StreamWriter(fs))
    {
        foreach (string item in lstFilesFound.Items)
        {
            sw.WriteLine(item);
        }
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!