目前,我将文件保存到代码中硬编码的目录中:
var filePath = Path.Combine(@"C:\users\my documents\github\project\source\project\App_Data\stored\", package.Id + ".zip");
但我需要使用 Server.MapPath ...保存我的文件。比如:
FileInfo userFile = new FileInfo(Path.Combine(Server.MapPath("~/App_Data/stored"), package.Id));
完整的功能:
public void CompressAndDeleteSources(FlinkeMailPackage package)
{
var filePath = Path.Combine(@"C:\users\my documents\github\project\source\project\App_Data\stored\", package.Id + ".zip");
using (ZipFile zipFile = new ZipFile(filePath))
{
foreach (var file in package.FlinkeMailFileList)
{
string bestandsNaam = @"C:\users\my documents\github\project\source\project\App_Data\uploads\" + file.OriginalName;
zipFile.AddFile(bestandsNaam);
}
zipFile.Save();
}
foreach (var file in package.FlinkeMailFileList)
{
var filePathToDelete = @"C:\users\my documents\github\project\source\project\App_Data\uploads\" + file.FileName;
File.Delete(filePathToDelete);
}
}
但是当我尝试使用Server.MapPath("~/App_Data/stored")
它时,它不知道服务器是什么
编辑
我可以像这样使用它: HttpContext.Current.Server.MapPath("~/App_Data/stored");
但我不能package.Id + ".zip"
像这样使用它: var savePath = HttpContext.Current.Server.MapPath("~/App_Data/stored"),package.Id + ".zip"));