我構建了一個程序,用於搜索由用戶(源文件夾)設置的文件夾內的所有.xml,並將所有這些文件複製到另一個文件夾(目標文件夾)。處理多個txt文件時出錯
我的方案是能夠搜索所有子文件夾中的所有XML從(來源文件夾),5000頁左右的文件結果返回第一個被放置在列表,這個列表後來被一個函數的工作,但他只能使用31個文件,然後出現「不響應」,調試器顯示該程序在執行中保持很長時間。
這裏是我的代碼:
按鈕動作:
private void btnCopiarSalvar_Click(object sender, EventArgs e)
{
foreach (string name in listFileNames)
{
if (readXML(name))
{
tbArquivo.Text = name ; //Feedback textbox, tell the current filename
}
}
pbStatus.Increment(50);
cbFinal.Checked = true; //Feedback checkBox, to tell user that the task is over.
}
功能ReadXML的
public bool readXML(string name)
{
//foreach (string nome in listaArquivos)
//{ //I tried to the foreach inside, but nothing Works.
try
{
string text = null;
string readBuffer = File.ReadAllText(name);
text = readBuffer.Aggregate(text, (current, b) => current + b);
var encoding = new ASCIIEncoding();
Byte[] textobytes = encoding.GetBytes(text);
if (!File.Exists(destino))
{
string destinoComNomeArquivo = destino + "\\" + Path.GetFileName(nome);
using (FileStream fs = File.Create(destinoComNomeArquivo))
{
foreach (byte textobyte in textobytes)
{
fs.WriteByte(textobyte);
pbProcess.PerformStep();
}
Console.WriteLine("Arquivo gravado " + Path.GetFileName(nome));
}
}
pbProcess.PerformStep();
}
catch (Exception e)
{
Console.WriteLine(e);
}
//}
return true;
}
錯誤:檢測ContextSwitchDeadlock。
嘗試解決方案:禁用託管調試助手。
禁用MDA後,程序仍然只讀取31個文件(5k)。
等待20分鐘後,我現在有127個.XML文件,所以我想我只是有等待......但我太沉重了,即使是英特爾i7。 – 2013-05-10 22:12:27
我可以使用File.copy來複制所有文件,但是我需要在文件末尾讀寫信息。所以我需要閱讀所有文件...悲傷。 – 2013-05-10 22:16:42
正如其他人所建議的那樣,使用'File.Copy'方法複製文件,讀取整個文件並將其寫入不同的文件效率極低。至於等待的問題,這是因爲讀/寫操作正在GUI上運行的同一線程上執行。把它放在一個'BackgroundWorker'中,讓它做好工作並更新進度條。 – PoweredByOrange 2013-05-10 22:18:30