C# 文件系统迭代器是否将结果写入CSV?

C# 文件系统迭代器是否将结果写入CSV?,c#,csv,iterator,iteration,C#,Csv,Iterator,Iteration,我正在尝试编写一个文件系统迭代器作为一个简单的Windows应用程序。我将发布我所拥有的。问题是,当我为迭代选择“C:\”时,应用程序会锁定。如果我从我的文档中选择一个目录,它就可以正常工作。我做错了什么?总体目标是将迭代结果写入csv文件。如果你也能帮忙,我将非常感激 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawi

我正在尝试编写一个文件系统迭代器作为一个简单的Windows应用程序。我将发布我所拥有的。问题是,当我为迭代选择“C:\”时,应用程序会锁定。如果我从我的文档中选择一个目录,它就可以正常工作。我做错了什么?总体目标是将迭代结果写入csv文件。如果你也能帮忙,我将非常感激

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace FileIterator
{
    class Iterator
    {

    public class MyItem
    {
        public static string it { get; set; }
    }

    public class Record
    {
        public long fileSize { get; set; }
        public string fileName { get; set; }

    }

    static List<Record> fileList = new List<Record>();

    public static void Iterate(string dir_tree)
    {
        Stack<string> dirs = new Stack<string>(20);

        if (!Directory.Exists(dir_tree))
        {
            MessageBox.Show("The directory you selected does not exist.", "Directory Selection Error",
            MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        }
        dirs.Push(dir_tree);

        while (dirs.Count > 0)
        {
            string currentDir = dirs.Pop();
            string[] subDirs;
            try
            {
                subDirs = Directory.GetDirectories(currentDir);
            }

            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("You do not have permission to access this folder", "Directory Permission Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                continue;
            }

            catch (DirectoryNotFoundException)
            {
                MessageBox.Show("The current directory does not exist", "Directory Not Found",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                continue;
            }

            string[] files = null;

            try
            {
                files = System.IO.Directory.GetFiles(currentDir);
            }

            catch (UnauthorizedAccessException)
            {
                MessageBox.Show("You do not have permission to access this folder", "Directory Permission Error",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                continue;
            }

            catch (DirectoryNotFoundException)
            {
                MessageBox.Show("The current directory does not exist", "Directory Not Found",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                continue;
            }



            foreach (string file in files)
            {
                try
                {
                    FileInfo fi = new FileInfo(file);
                    fileList.Add( new Record {
                        fileName = fi.Name,
                        fileSize = fi.Length
                    });
                }

                catch (FileNotFoundException)
                {
                    MessageBox.Show("The current file does not exist" + file, "File Not Found",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    continue;
                }
            }

            foreach (string str in subDirs)
                dirs.Push(str);
        }




    }

}

即使在确定目录树不存在后,仍将其推送到堆栈上。我认为这是你的主要问题:

    if (!Directory.Exists(dir_tree))
            {
                MessageBox.Show("The directory you selected does not exist.", "Directory Selection Error",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
// maybe you should return or something here....
            }
            dirs.Push(dir_tree);

您正在将
目录树
推送到堆栈上,即使您确定它不存在。我认为这是你的主要问题:

    if (!Directory.Exists(dir_tree))
            {
                MessageBox.Show("The directory you selected does not exist.", "Directory Selection Error",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
// maybe you should return or something here....
            }
            dirs.Push(dir_tree);

我认为这里的主要问题是,您试图从一开始就将所有内容加载到堆栈中。您的程序可能没有冻结,它只是在不断地尝试递归地遍历您机器上的数千个文件和文件夹。这将需要一段很长的时间


为什么不边写边写CSV文件,这样至少可以看到进度?您可能还希望使用,这样您就可以保持UI的响应性,并在算法通过文件系统时显示一些进展。

我认为这里的主要问题是,您试图从一开始就将所有内容加载到堆栈中。您的程序可能没有冻结,它只是在不断地尝试递归地遍历您机器上的数千个文件和文件夹。这将需要一段很长的时间


为什么不边写边写CSV文件,这样至少可以看到进度?您可能还希望使用,这样您可以保持UI的响应性,并在算法通过文件系统运行时显示一些进度。

在C:\上尝试此操作将花费很长时间,有很多文件,可能需要几分钟的时间来处理,UI将一直处于冻结状态。如上所述,如果你真的想这样做,最好的计划就是找一个后台工作人员

其他评论

这里的堆叠安排很麻烦,需要吗?(这是一个家庭作业/培训练习吗?)一个简单的想法是通过在每一级重新调用Iterate来递归树

e、 g

私有类文件迭代器{
公共IEnumerable迭代(字符串路径){
var currentFiles=新列表(
Directory.GetFiles(path).Select(file=>{
var fi=新文件信息(文件);
返回新记录{FileName=fi.Name,FileSize=fi.Length};
}
));
var childFiles=Directory.GetDirectories(path).SelectMany(dir=>Iterate(dir));
返回currentFiles.Union(childFiles);
}
}
注:

我省略了安全检查,只是为了加快我的编码速度,您可能仍然需要检查它们。目录未找到检查,尽管我对它有怀疑?文件系统内容是否可能在程序执行期间发生更改?这是一个经常发生的事件吗

另外,这里的Messagebox调用也不好。他们破坏了任何自动化单元测试的尝试

hth,

Alan。

在C:\上尝试此操作将花费很长时间,有很多文件,处理可能需要几分钟,UI将一直处于冻结状态。如上所述,如果你真的想这样做,最好的计划就是找一个后台工作人员

其他评论

这里的堆叠安排很麻烦,需要吗?(这是一个家庭作业/培训练习吗?)一个简单的想法是通过在每一级重新调用Iterate来递归树

e、 g

私有类文件迭代器{
公共IEnumerable迭代(字符串路径){
var currentFiles=新列表(
Directory.GetFiles(path).Select(file=>{
var fi=新文件信息(文件);
返回新记录{FileName=fi.Name,FileSize=fi.Length};
}
));
var childFiles=Directory.GetDirectories(path).SelectMany(dir=>Iterate(dir));
返回currentFiles.Union(childFiles);
}
}
注:

我省略了安全检查,只是为了加快我的编码速度,您可能仍然需要检查它们。目录未找到检查,尽管我对它有怀疑?文件系统内容是否可能在程序执行期间发生更改?这是一个经常发生的事件吗

另外,这里的Messagebox调用也不好。他们破坏了任何自动化单元测试的尝试

hth,

Alan。

尝试单步执行调试器中的代码…可能重复尝试单步执行调试器中的代码…可能重复我非常喜欢在程序运行时编写CSV的想法。我该怎么做?真的很简单。不要创建记录实例,只需将所需数据写入文件即可。您可能希望创建一个
FileStream
,并在迭代函数开始时将其包装在
StreamWriter
中,然后在整个函数中使用它。请查看StreamWriter.WriteLine:。最后,请确保在最后使用
Flush()
流。我非常喜欢在程序运行时编写CSV的想法。我该怎么做?真的很简单。不要创建记录实例,只需将所需数据写入文件即可。您可能希望创建一个
FileStream
,并在迭代函数开始时将其包装在
StreamWriter
中,然后在整个函数中使用它。请查看StreamWriter.WriteLine:。最后,请确保在最后刷新流。这只是一个训练练习。目前我是一名UNIX的C程序员,我正试图找到一份新工作,我在C#几乎是个呆子,我只是在尝试使用这个pr
private class FileIterator {

    public IEnumerable<Record> Iterate(string path) {

        var currentFiles = new List<Record>(
            Directory.GetFiles(path).Select(file => {
                var fi = new FileInfo(file);
                return new Record{FileName = fi.Name, FileSize = fi.Length};
            }
        ));

        var childFiles = Directory.GetDirectories(path).SelectMany(dir => Iterate(dir));
        return currentFiles.Union(childFiles);

    }
}