问题
在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) 在 System.IO.FileStream…ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) 在 System.IO.FileStream…ctor(String path, FileMode mode) 在
问题分析
一般先看是不是路径问题,我这里遇到的是路径问题
原先我的代码是:
path = $"{dstFilePath}/{DateTime.Now.ToString("yyyyMMdd")}";
if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(dstFilePath)))
{
Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(dstFilePath));
}
path = $"{path}/{dstFileName}";
FileStream outfile = new FileStream(System.Web.HttpContext.Current.Server.MapPath(path), FileMode.Create);
wk.Write(outfile);
outfile.Close();
但是我得路径上是加了DateTime.Now.ToString("yyyyMMdd")
文件夹,但是我判断创建的只有dstFilePath
,因为找不到日期文件夹,所有造成报错
我把他改成创建path
,就没问题了
path = $"{dstFilePath}/{DateTime.Now.ToString("yyyyMMdd")}";
if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(path)))
{
Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(path));
}
path = $"{path}/{dstFileName}";
FileStream outfile = new FileStream(System.Web.HttpContext.Current.Server.MapPath(path), FileMode.Create);
wk.Write(outfile);
outfile.Close();
总结:一般出现这种问题都是路径问题,看看文件路径是否存在