我想,如果它包含特定字符串移動文件,代碼如下移動在C#中的文本文件,如果它包含一個值
foreach (FileInfo file in files)
{
//reads the file contents
string MessageContents = File.ReadAllText(file.FullName);
//checks if the textwords are present in the file
foreach (string Keyword in textwords)
{
//if they are file is moved to quarantine messages
if (MessageContents.Contains(Keyword))
{
try
{
File.Move(file.FullName, File_quarantine);
}
catch (IOException cannot_Move_File)
{
MessageBox.Show("The process has failed: {0}", cannot_Move_File.ToString());
}
break;
}
//else it is moved To valid messages
else
{
try
{
File.Move(file.FullName, File_Valid);
}
catch (IOException cannot_Move_File)
{
MessageBox.Show("The process has failed: {0}", cannot_Move_File.ToString());
}
break;
}
}
}
}
但過程總是失敗,錯誤A first chance exception of type 'System.IO.IOException' occurred in mscorlib.dll
我不確定爲什麼發生這種情況,任何幫助將不勝感激。
你仍然對文件上的鎖,因爲你開了一個流給它。移動文件移出文件讀數的邏輯。看到我的答案。 – Kolky