C# “ASP.Net核心”;System.IO.DirectoryNotFoundException;在一些网站上,但在其他网站上没有
在某些项目上创建json文件的代码在所有新项目上都失败了 以管理员身份运行没有帮助 救命啊 我创建的所有新ASP.Net核心MVC项目都无法在本地目录中创建文件。失败的代码是:C# “ASP.Net核心”;System.IO.DirectoryNotFoundException;在一些网站上,但在其他网站上没有,c#,asp.net,C#,Asp.net,在某些项目上创建json文件的代码在所有新项目上都失败了 以管理员身份运行没有帮助 救命啊 我创建的所有新ASP.Net核心MVC项目都无法在本地目录中创建文件。失败的代码是: string _physicalPath = Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "Files"), "tblTxt.json"); using (StreamWriter writer = System.IO.File.AppendTex
string _physicalPath = Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "Files"), "tblTxt.json");
using (StreamWriter writer = System.IO.File.AppendText(_physicalPath))
{
writer.WriteLine("log message");
}
所有新项目的结果如下:
{System.IO.DirectoryNotFoundException:找不到路径“C:\Program Files\IIS Express\Files\tblTxt.json”的一部分。
位于System.IO.FileStream.ValidateFileHandle(安全文件句柄)
在System.IO.FileStream.CreateFileOpenHandle(文件模式、文件共享、文件选项)
位于System.IO.FileStream..ctor(字符串路径、文件模式、文件访问访问、文件共享、Int32 bufferSize、文件选项)
在System.IO.StreamWriter..ctor(字符串路径、布尔追加、编码、Int32 bufferSize)
位于System.IO.StreamWriter..ctor(字符串路径,布尔追加)
位于System.IO.File.AppendText(字符串路径)
在C:\Users\patri\source\repos\WebAutomation\WebAutomation\Startup.cs:line 25}中的WebAutomation.Startup.Configure(IApplicationBuilder应用程序,IHostingEnvironment环境)处
Path.Combine(Path.Combine(Directory.GetCurrentDirectory(),“文件”)不好
像这样传递字符串给它
var t = Path.Combine("D:", "DOWNLOADS","abc");//==>D:\DOWNLOADS\abc
其次是Directory.GetCurrentDirectory()给您
“C:\Program Files\IIS Express\Files\tblTxt.json”
Check if "Files" directory exists in "C:\Program Files\IIS Express" using
"Directory.Exists()"
if it dosent exist then create one using "Directory.CreateDirectory()"
Directory.CreateDirectory(@"C:\Program Files\IIS Express\Files")
首先,我要确保正在访问的目录具有适当的权限。将IIS\u IUSR添加到这些文件夹的安全性中。您是在本地计算机上打开文件,还是在远程计算机或文件服务器上打开文件?问题可能是组策略不允许您在远程计算机上打开文件。通常情况下,无法打开n您使用G$的远程文件,其中美元符号表示您正在使用管理员权限。请参阅:IUSR对本地计算机上的文件夹拥有完全控制权限。根据您的想法,我将代码更改为:string logfilePath=Path.Combine(env.ContentRootPath,“logs”,“tblTxt.json”);stringdirectoryPath=Path.GetDirectoryName(logfilePath);if(!Directory.Exists(directoryPath)){Directory.CreateDirectory(directoryPath);}嗨,它起作用了吗?[如果您使用“C:\Program Files\”作为env.ContentRootPath,在win 10中,我们没有太多的访问权限“C:\Program Files\”如果我们想访问它,我们必须拥有它的所有权……]