【问题标题】:File not found exception in Windows Service在 Windows 服务中找不到文件异常
【发布时间】:2013-07-09 07:33:20
【问题描述】:

我在 Windows 服务中编写了以下代码。当我尝试保存 zip 文件时,我收到了 File Not Found 异常。我在zipFile.AddFile 中获取文件名。

System.IO.MemoryStream ms1 = new System.IO.MemoryStream();
System.IO.StreamWriter writer = new System.IO.StreamWriter(ms1, Encoding.UTF8);

string strHeader = "";
if (FailedErrorLogList != null && FailedErrorLogList.Any())
{
   strHeader += "file_name" + ",";
   strHeader += "mobile_no" + ",";
   strHeader += "Description" + ",";
   writer.WriteLine(strHeader);
   foreach (Transactionapierrorfailedlog ErrorLog in FailedErrorLogList)
   {
      string strRowValue = "";
      strRowValue += Escape(ErrorLog.file_name) + ",";

      strRowValue += Escape(ErrorLog.mobile_no) + ",";

      strRowValue += Escape(ErrorLog.Description) + ",";

      writer.WriteLine(strRowValue);
      // writer2.WriteLine(strRowValue);
   }
   writer.Flush();
   ms1.Position = 0;
}

String filename = "Hello.csv";

if (FailedErrorLogList != null && FailedErrorLogList.Any())
{
   ZipFile zipFile = new ZipFile();
   using (zipFile)
   {
      zipFile.AddFile(filename);
      zipFile.Save("Hello.zip");

   }
}

【问题讨论】:

  • 尝试指定Hello.csv的完整路径
  • 这是一个动态创建的文件。
  • 我不确定ZipFile类是这样使用的,看这里msdn.microsoft.com/en-us/library/… PS:链接已编辑,现在是eng​​span>
  • @WannyMiarelli:可能是另一个类(可能是 DotNetZip)。
  • @JeffRSon 我认为是这样,但未指定。告诉我们您正在使用什么库,它会有所帮助。

标签: c# .net windows-services filenotfoundexception


【解决方案1】:

您的服务进程的“当前目录”不是您所期望的(事实上,它很可能不是您的 .exe 的路径 - 它可能类似于 c:\windows\system32)。您始终需要准确指定(绝对路径)要在哪里创建文件以及从哪里读取。

【讨论】:

  • 感谢 JeffRSon 的回复。我正在创建 csv 文件并将其保存在内存流中。有什么办法可以解决它..
  • 你到底是哪里出了问题?
【解决方案2】:

使用System.Windows.Forms.Application.StartupPath 而不是Enviroment.CurrentDirectory

【讨论】:

    猜你喜欢
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 2012-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多