[英]File not found exception in Windows Service
我在 Windows 服务中编写了以下代码。 当我尝试保存 zip 文件时,出现文件未找到异常。 我在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");
}
}
您的服务进程的“当前目录”不是您所期望的(实际上,它很可能不是您的 .exe 的路径 - 它可能类似于c:\\windows\\system32
)。 您始终需要准确指定(绝对路径)要创建文件的位置以及要读取的位置。
使用System.Windows.Forms.Application.StartupPath
而不是Enviroment.CurrentDirectory
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.