我正在尝试从控制台应用程序中的大型制表符分隔文本文件中读取后编写文本文件。问题是,如果我在服务器上同时运行此exe的许多实例,它会在TextWriter.WriteLine中给出运行时错误,然后由于未处理的异常而导致应用程序崩溃。所有实例都会发生这种情况。我无法理解这种行为的原因..是不是因为我没有使用会动态使用内存的StringBuilder?
我的代码如下:
StreamReader sr = new StreamReader(@FilePath, System.Text.Encoding.Default);
string mainLine = sr.ReadLine();
string[] fileHeaders = mainLine.Split(new string[] { "\t" }, StringSplitOptions.None);
string newLine = "";
System.IO.StreamWriter outFileSw = new System.IO.StreamWriter(@outFile);
while (!sr.EndOfStream)
{
mainLine = sr.ReadLine();
string[] originalLine = mainLine.Split(new string[] { "\t" }, StringSplitOptions.None);
newLine = "";
for (int i = 0; i < fileHeaders.Length; i++)
{
if(fileHeaders[i].Trim() != "")
newLine = newLine + fileHeaders[i].Trim() + "=" + originalLine[i].Trim() + "&";
}
outFileSw.WriteLine(newLine.Remove(newLine.Length - 1));
FileInfo fileInfo = new FileInfo(@outFile);
if (fileInfo.Length > (1.3 * 1024.0 * 1024.0 * 1024.0)) // greater than 1.3 GB
{
outFileSw.Close();
outFileNumber = outFileNumber + 1;
outFileSw = new System.IO.StreamWriter(@outFile + outFileNumber.ToString() + ".txt");
}
}
outFileSw.Dispose();
sr.Close();
sr.Dispose();
错误详情如下:
Exception Message: The specified network name is no longer available.
Stack Trace: at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.FileStream.Write(Byte[] array, Int32 offset, Int32 count)
at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean flushEncoder)
at System.IO.StreamWriter.Write(Char[] buffer, Int32 index, Int32 count)
at System.IO.TextWriter.WriteLine(String value)
at ExampleExe.ExampleProcess.FnFiles()
Unhandled Exception: System.IO.IOException: The specified network name is no longer available.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.WriteCore(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.FileStream.FlushWrite(Boolean calledFromFinalizer)
at System.IO.FileStream.Dispose(Boolean disposing)
at System.IO.FileStream.Finalize()
谢谢, 卡努