首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >一些站点中的ASP.Net核心"System.IO.DirectoryNotFoundException“,而不是其他站点

一些站点中的ASP.Net核心"System.IO.DirectoryNotFoundException“,而不是其他站点
EN

Stack Overflow用户
提问于 2019-02-05 01:16:07
回答 1查看 3.4K关注 0票数 0

创建在某些项目上工作的json文件的代码在所有新项目上都会失败。

以管理员身份运行无济于事。

请帮帮我!

我创建的所有新的ASP.Net核心MVC项目都无法在本地目录中创建文件。失败的代码是:

代码语言:javascript
复制
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(SafeFileHandle模式)在System.IO.FileStream.CreateFileOpenHandle(FileMode模式,FileShare共享,FileOptions选项)在System.IO.FileStream..ctor(字符串路径,FileMode模式,FileAccess访问,FileShare共享,Int32 bufferSize,FileOptions选项)在System.IO.StreamWriter..ctor(字符串路径,布尔附加,编码编码,Int32 bufferSize)在System.IO.StreamWriter..ctor(字符串路径,布尔附加)在System.IO.File.AppendText(字符串路径)在app,C:\Users\patri\source\repos\WebAutomation\WebAutomation\Startup.cs:line 25中的IHostingEnvironment环境)}

EN

回答 1

Stack Overflow用户

发布于 2019-02-05 02:42:59

首先,Path.Combine(Path.Combine(Directory.GetCurrentDirectory(),“文件”)不是很好的传递字符串,如下所示

代码语言:javascript
复制
var t = Path.Combine("D:", "DOWNLOADS","abc");//==>D:\DOWNLOADS\abc

其次,Directory.GetCurrentDirectory()会给出'C:\Program Files\IIS Express\Files\tblTxt.json‘。

代码语言:javascript
复制
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")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54521156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档