まず、すべて (リーダーとライターの両方) を適切に破棄して閉じるようにしました。私が行っている方法は、using ステートメントを使用していることです。この前は、ライターとリーダーの両方が提供する破棄およびクローズ メソッドを手動で使用していました。これらの方法はどちらも問題を解決しません。第二に、私が疑っている点でもありますが、読み取りと書き込みの両方で多くのファイルを処理しています。さらに、プログラムを実行するたびに、プログラムは処理されるはずのファイルのリストをさらに深く実行します (つまり、10 個のファイルがある場合、最初にプログラムを実行すると、最初の 2 つが処理されてから、エラー; プログラムを 2 回目に実行すると、エラーをスローする前に最初の 5 が処理されます)。では、この問題/バグをどのように修正すればよいでしょうか? 前もって感謝します!
編集 - コード添付
public static void FileProcessing(string initPath, string targetPath, string startLine)
{
string line = null;
string content = null;
string nl = "\r\n";
using (StreamReader reader = new StreamReader(initPath))
{
while (!reader.ReadLine().Contains(startLine)) ;
while (!reader.EndOfStream)
{
line = reader.ReadLine();
// Ignore empty line.
if (line.Length > 0)
{
if (!line.Contains("<div"))
content += line += nl;
else
break;
}
}
}
using (StreamWriter writer = new StreamWriter(targetPath))
{
writer.Write(content.Trim());
}
}
/// <summary>
/// Create a temporary text file that correspond to the calendar department page
/// and return the path.
/// </summary>
public static string CreateFile(string path, string title)
{
string npath = path + title + ".txt";
if (!File.Exists(npath))
File.CreateText(npath);
return npath;
}
/// <summary>
/// Return the title of the page.
/// </summary>
/// <param name="path"></param>
public static string GetTitle(string path)
{
string line;
using (StreamReader reader = new StreamReader(path))
{
for (int i = 0; i < 31; ++i)
{
line = reader.ReadLine();
// Ignore empty line.
if (line.Length > 0)
{
if (line.Contains("<h1>"))
{
return line.Slice(4, -5);
}
}
}
return "null";
}
}
/// <summary>
/// Get content from temp path and
/// output the processed text to destination path.
/// </summary>
/// <param name="tempPath"></param>
/// <param name="title"></param>
public static void WriteProcessedText(string tempPath, string targetPath)
{
string content = null;
using (StreamReader reader = new StreamReader(tempPath))
{
string nl = "\r\n";
string line = null;
while (!reader.EndOfStream)
{
line = HtmlRemoval.StripTagsRegex(reader.ReadLine()) + nl;
if (line.Contains(" "))
line = line.Replace(" ", " ");
content += line;
}
}
using (StreamWriter writer = new StreamWriter(targetPath))
{
writer.Write(content.Trim());
}
}