Console.Writeline抛出“没有足够的存储空间来处理此命令。”IO异常

时间:2014-11-27 06:43:18

标签: c# out-of-memory console-application ioexception console.writeline

我有一个应用程序可以将1000个文件从一个文件夹复制到另一个文件夹。复制每个文件后,我们会将复制成功/失败信息写入单独的文件中。

有时,在将此副本信息写入文件时,StreamWriter.WriteLine会引发以下异常:

  

10/28/2014 12.21.02.068消息:没有足够的存储空间可用于处理此命令。   文件名:将文件C:\ Program Files \ XYZ \ SampleFile.xml复制到C:\ Program   Files \ ABC \ SampleFile.xml.xml |内部例外:|键入:System.IO.IOException |资料来源:mscorlib

Not enough storage is available to process this command.

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__ConsoleStream.Write(Byte[] buffer, 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 System.IO.TextWriter.SyncTextWriter.WriteLine(String value)
   at System.Console.WriteLine(String value)
   at VersionActivator.VersionActivationController.LogMessage(String message)                            
mscorlib : Not enough storage is available to process this command.**

但是当PC在安全模式下初始化时,此应用程序正常工作。

我的代码:

foreach (FileInfo fi in source.GetFiles())
{
    if (fi.Exists)
    {
        string destFileName = Path.Combine(target.FullName, fi.Name);

        // If the file is readonly, make it non-readonly
        if (File.Exists(destFileName))
        {
             File.SetAttributes(destFileName, FileAttributes.Normal); // IMS 2793051209
        }
        LogMessage(string.Format("Copying file {0} to {1}", fi.FullName, destFileName));
        fi.CopyTo(destFileName, true);
    }
}

public void LogMessage(string message)
{
    try
    {
        **Console.WriteLine(message); // This is the place am getting IO exception**
        if (m_LogWriter != null)
        {
            m_LogWriter.WriteLine(
              string.Format(@"{0} {1}"
              , DateTime.Now.ToString("MM/dd/yyyy HH.mm.ss.fff")
              , message)
            );
        }
    }
    catch (Exception ex)
    {
        WriteException(ex, message);
              throw;
    }
}

0 个答案:

没有答案