简体   繁体   English

从appsetting.json读取文件路径

[英]Read the file path from appsetting.json

I have a webapi developed in asp.net core.我有一个在 asp.net 内核中开发的 webapi。 From within the code I need to create xml files and save it to a shared drive.I am having a problem because of the path with escape sequence.在代码中,我需要创建 xml 个文件并将其保存到共享驱动器。由于带有转义序列的路径,我遇到了问题。

In Json, the c# verbatim does not work, so to specify path I need to use "\" so my path becomes \root\folder1\folder2, when I read this in c#, and then try to replace the "\" with "" it does not work, it removes both the slashes.在Json中,c#逐字不起作用,所以要指定路径我需要使用“\”所以我的路径变成了\root\folder1\folder2,当我在c#中读取这个时,然后尝试将“\”替换为“ " 它不起作用,它删除了两个斜杠。 I tried using a different character like a '+' or '-' and replace that with the @"" it still adds two backslashes.我尝试使用不同的字符,如 '+' 或 '-' 并将其替换为 @"" 它仍然添加两个反斜杠。 When there are two backslashes in a path windows does not find the path.当路径中有两个反斜杠时 windows 找不到路径。 so its not working.所以它不工作。 Nothing seems to work.似乎没有任何效果。

I tried to use forward slash \root/folder1/folder2 and then split based on the forward slash and build the path again with the array but even here it adds the two backslashes我尝试使用正斜杠 \root/folder1/folder2 然后根据正斜杠拆分并再次使用数组构建路径但即使在这里它也添加了两个反斜杠

  var path = directoryList[0].Value.Split('/');
                foreach (string item in path)
                {
                    filePath +=(string.Format(@"{0}\",item));
                }

Any advice on this??对此有何建议?? How to read the path from appsetting.json and remove the double backslashes so that the path is recoginised and the file is created.如何从 appsetting.json 读取路径并删除双反斜杠,以便识别路径并创建文件。

you don't need to use Verbatim identifier when the string is correctly use the escape characters当字符串正确使用转义字符时,您不需要使用逐字标识符

use it directly without '@' or if you need to add some root first use the path combine直接使用它而不使用'@',或者如果您需要先添加一些根目录,请使用路径组合

string path = directoryList[0].Value; // use this dirctly if it full qulified path
string fullPath = Path.Combine(@"c:\anyRoot",path); //add some root if it a partial path

Have you considered making your life easier and just swearing off the back-slash character file path separated made popular by MS-DOS in 1981?您是否考虑过让您的生活更轻松,只是发誓放弃由 MS-DOS 在 1981 年流行的反斜杠字符文件路径? It was an unfortunate choice at the time and it is always causing difficulties.这在当时是一个不幸的选择,而且总是造成困难。

You can safely use "/" as a path separator in a Windows file path so您可以安全地使用“/”作为 Windows 文件路径中的路径分隔符,因此

   var streamReader = new StreamReader("/Users/gbsil/src/filepath/hello.txt");

is equivalent to相当于

   streamReader = new StreamReader("\\Users\\gbsil\\src\\filepath\\hello.txt");

and also equivalent to也相当于

streamReader = new StreamReader(@"\Users\gbsil\src\filepath\hello.txt");

If you want to have a drive letter如果你想要一个驱动器号

then然后

streamReader = new StreamReader("c:/Users/gbsil/src/filepath/hello.txt");

works the same as工作方式与

streamReader = new StreamReader("c:\\Users\\gbsil\\src\\filepath\\hello.txt");

Check out the documentation here: File path formats on Windows Systems在此处查看文档: Windows 系统上的文件路径格式

Historical note: Unix always used a forward slash '/' as a separator as did the C language so the back slash '' always conflicted with this.历史记录:Unix 总是使用正斜杠“/”作为分隔符,就像 C 语言一样,所以反斜杠“”总是与此冲突。 The '' is used as an escape character in the C language, which made matters even worse - to put a '' into a string you needed to write '\'. '' 在 C 语言中用作转义字符,这使事情变得更糟 - 将 '' 放入需要写入 '\' 的字符串中。 Programs that needed to run multiple platforms, like JavaScript used the C language convention of '/' separators and '' escape characters because they were written in C.需要运行多个平台的程序,如 JavaScript 使用 C 语言约定的“/”分隔符和“”转义字符,因为它们是用 Z0D61F8370CAD1D412F80B84D143E125 编写的。 There are sometimes reasons to use the back slash file path separator, but your life will be easier if you only do so when forced.有时使用反斜杠文件路径分隔符是有原因的,但如果你只在强制时这样做,你的生活会更轻松。

I got it to work..so I was using the xmldoc.Save() and this was by default taking the local directory root, so my path was going as D:/localfoldername\shared network path\.我让它工作了..所以我使用的是 xmldoc.Save() 并且默认情况下使用本地目录根目录,所以我的路径是 D:/localfoldername\shared network path\。 So, this line seemed to have fixed it所以,这条线似乎已经修复了它
xmldoc.Save("\{0}",path) , now the path goes as \shared Network Path\ and my file is saving fine. xmldoc.Save("\{0}",path) ,现在路径为 \shared Network Path\ 并且我的文件保存得很好。

thanks for all the inputs.感谢所有的投入。

for do.net core 6对于 do.net 核心 6

  1. Install serilog.extensions.logging.file on PM在 PM 上安装 serilog.extensions.logging.file

  2. in Program.cs在 Program.cs 中

app.Services.GetService().AddFile(builder.Configuration["Logging:LogFilePath"].ToString());
  1. in appsettings.json在 appsettings.json
"Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    },
    "LogFilePath": "D:\\Logs\\CSMonitor_{Date}.txt"
}
   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Visual Studio 2017点网核心的Angular 4应用程序。 如何从打字稿文件访问appsetting.json配置密钥 - angular 4 application with Visual studio 2017 dot net core. how to access appsetting.json configuration key from typescript file 将值保存到 appsetting..NET 核心中的 json - Saving values to appsetting.json in .NET Core appsetting.json 中的变量如何与实际值交换? - how does a variable in appsetting.json get swapped with actual value? Appsetting.json 说没有找到尝试使用 Blazor WebAssembly - Appsetting.json saying not found trying to use a Blazor WebAssembly 从 IConfiguration 中的其他 appsetting 文件读取问题 - Problem to read from other appsetting file in IConfiguration 如何使用 c# 获取或设置自定义 appsetting.json - how can I get or set custom appsetting.json with c# appsettings.json 和 appsettings.Release.json 文件在为.Netcore3.1 项目运行 Azure DevOps CI 管道后未合并 - appsetting.json and appsettings.Release.json files are not merging after ran the Azure DevOps CI pipeline for .Netcore3.1 project 如何使用kotlin从路径读取json文件 - How to read json file from path using kotlin 如何使用d3从json文件读取路径信息 - how to read path info from json file using d3 从文件读取JSON - read JSON from file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM