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

时间:2014-11-11 11:55:16

标签: ioexception streamwriter.write

我有一个应用程序可以将1000个文件从一个文件夹复制到另一个文件夹。复制每个文件后,我们会将复制成功/失败信息写入单独的文件中。有时,在将此副本信息写入文件时,StreamWriter,WriteLine会抛出以下异常“

10/28/2014 12.21.02.068 Message : Not enough storage is available to process this command.
Filename : Copying file C:\Program Files\XYZ\SampleFile.xml to C:\Program 
Files\ABC\SampleFile.xml.xml | Inner Exception :  | Type : System.IO.IOException | Source : 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在安全模式下初始化时,此应用程序正常工作。

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

它是用C#开发的。

我的代码是

private void CopyAll(DirectoryInfo source, DirectoryInfo target)
      {
          try
          {
              if (!source.Exists)
              {
                  LogMessage(string.Format("{0} does not exist!", source.FullName));
                  return;
              }
              // Check if the target directory exists, if not, create it.
              if (Directory.Exists(target.FullName) == false)
              {
                  Directory.CreateDirectory(target.FullName);
              }

              // Copy each file into it's new directory.
              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);
                  }
              }
          }
          catch (Exception ex)
          {
              System.Diagnostics.Trace.WriteLine(ex.ToString());
              throw;
          }
      }

public void LogMessage(string message)
      {
          try
          {
              Console.WriteLine(message);
              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;
          }
      }

m_LogWriter将在Windows_load事件中初始化。

if (m_LogWriter != null)
            return;

         DirectoryInfo logFolder = new DirectoryInfo(versionActivatorConfigObj.Folders.LogFolder.Path);

         try
         {
            if(!(logFolder.Exists))
            {
               logFolder.Create();
            }
            logFolder.Refresh();
            if(logFolder.Exists)
            {
               string logFileName = string.Format(@"MyCopyLog{0}.txt", DateTime.Now.ToString("M-d-yyyy_h-m-s"));
               m_LogWriter = new StreamWriter(Path.Combine(logFolder.FullName, logFileName), true);
               m_LogWriter.AutoFlush = true;
            }
         }