问题描述
下面是该方案:
我有2+万个文件的目录。我有下面的代码写入的所有文件,在大约90分钟。是否有人有办法加快速度或使代码比较有效?我也想只写了在列表中的文件名。
I have a directory with 2+ million files. The code I have below writes out all the files in about 90 minutes. Does anybody have a way to speed it up or make this code more efficent? I'd also like to only write out the file names in the listing.
string lines = (listBox1.Items.ToString());
string sourcefolder1 = textBox1.Text;
string destinationfolder = (@"C:\anfiles");
using (StreamWriter output = new StreamWriter(destinationfolder + "\\" + "MasterANN.txt"))
{
string[] files = Directory.GetFiles(textBox1.Text, "*.txt");
foreach (string file in files)
{
FileInfo file_info = new FileInfo(file);
output.WriteLine(file_info.Name);
}
}
慢下来的是,它在写出1号线一时间。
The slow down is that it writes out 1 line at a time.
大约需要13-15分钟,让所有需要写出来的文件。
It takes about 13-15 minutes to get all the files it needs to write out.
下面75分钟是创建该文件。
The following 75 minutes is creating the file.
推荐答案
这可以帮助,如果你不做出一个FileInfo实例每个文件使用Path.GetFileName来代替:
It could help if you don't make a FileInfo instance for every file, use Path.GetFileName instead:
string lines = (listBox1.Items.ToString());
string sourcefolder1 = textBox1.Text;
string destinationfolder = (@"C:\anfiles");
using (StreamWriter output = new StreamWriter(Path.Combine(destinationfolder, "MasterANN.txt"))
{
string[] files = Directory.GetFiles(textBox1.Text, "*.txt");
foreach (string file in files)
{
output.WriteLine(Path.GetFileName(file));
}
}
这篇关于C#目录列表大规模目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!