简体   繁体   中英

Read the file path from appsetting.json

I have a webapi developed in asp.net core. 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.

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. 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. 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

  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.

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? 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

   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

Historical note: Unix always used a forward slash '/' as a separator as did the C language so the back slash '' always conflicted with this. 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 '\'. 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. 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\. 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.

thanks for all the inputs.

for do.net core 6

  1. Install serilog.extensions.logging.file on PM

  2. in Program.cs

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM