文件系统迭代程序将结果写入CSV
本文关键字:CSV 结果 迭代 程序 文件系统 | 更新日期: 2023-09-27 18:21:49
我正试图将文件系统迭代器作为一个简单的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);
}
}
Form.cs
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;
namespace FileIterator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void splitContainer1_Panel2_Paint(object sender, PaintEventArgs e)
{
}
string directory = " ";
private void button1_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
directory = folderBrowserDialog1.SelectedPath;
DirectoryName.Text = folderBrowserDialog1.SelectedPath;
Iterator.Iterate(directory);
}
}
}
即使在确定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文件,这样你至少可以看到进展?您可能还想使用BackgroundWorker,这样您就可以保持UI的响应,并在算法通过文件系统工作时显示一些进展。
在C:''上尝试这将需要很长时间,因为有很多文件,可能需要几分钟的时间来处理,所有时间UI都将被冻结。如上所述,如果你真的想做这件事,一个幕后工作人员是最好的计划。
其他评论
这里的堆叠安排很麻烦,你需要吗?(这是一个家庭作业/训练练习吗?)一个更简单的想法是通过在每个级别的上重新调用Iterate来向下递归
例如
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);
}
}
备注:
我省略了安全检查,只是为了加快我的编码速度——你可能仍然需要检查它们。目录没有找到检查虽然我是可疑的?在执行程序的过程中,文件系统内容是否可能发生变化?这种情况会经常发生吗?
此外,Messagebox调用在这里也不好。他们把自动化单元测试的任何尝试都搞砸了。
hth,
阿兰。