1

我在 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");

   }
}
4

2 回答 2

0

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

于 2013-07-09T08:20:42.940 回答
0

使用System.Windows.Forms.Application.StartupPath代替Enviroment.CurrentDirectory

于 2021-05-16T10:00:35.897 回答