admin管理员组

文章数量:1318563

I have a file sitting under ....debug\net8.0\files\file.txt

To get to this file i have the below code

var dirLoc = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location); 
var path = Path.Combine(dirLoc!, "\\files\\file.csv");

The problem i have is the path is never found. I have changed double slash (\\) to single \ and have tried to use .Replace(@"\\", @"\"); but even then the path is not found.

I then copied the path and then entered in my local Explorer window which said it couldnt find it but if i removed the two slashes into 1 then it worked.

I read around and have tried different code to get this working but i think i maybe missing something small? I have tried escaping the code but either im doing this wrong or have missed something.

Can anyone point me in the right direction.

I have a file sitting under ....debug\net8.0\files\file.txt

To get to this file i have the below code

var dirLoc = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location); 
var path = Path.Combine(dirLoc!, "\\files\\file.csv");

The problem i have is the path is never found. I have changed double slash (\\) to single \ and have tried to use .Replace(@"\\", @"\"); but even then the path is not found.

I then copied the path and then entered in my local Explorer window which said it couldnt find it but if i removed the two slashes into 1 then it worked.

I read around and have tried different code to get this working but i think i maybe missing something small? I have tried escaping the code but either im doing this wrong or have missed something.

Can anyone point me in the right direction.

Share Improve this question edited Jan 21 at 10:54 marc_s 756k184 gold badges1.4k silver badges1.5k bronze badges asked Jan 21 at 10:52 He-ManHe-Man 291 silver badge6 bronze badges 3
  • 3 Starting a path with \ makes it an absolute path (ie. relative to root of current drive). You need Path.Combine(dirloc, "files\\file.csv"). – Richard Commented Jan 21 at 10:57
  • Thanks that did the trick! I spent an hour on this for a small fix – He-Man Commented Jan 21 at 11:04
  • Please consider using and accepting one of the answers using Path.Combine(dirloc, "files", "file.csv"). One feature of Path is that when used cross platform if will use forward or back-slashes as required on the target platform. – IV. Commented Jan 21 at 20:09
Add a comment  | 

2 Answers 2

Reset to default 0

When using Path.Combine, you are not supposed to add leading or trailing backslashes (\\). Leading backslash is an instruction to discard all path info and start from the root, and produces @"\files\file.csv".

This works:

string path = Path.Combine (dirLoc, "files\\file.csv");

Or this (for better cross platform support):

string path = Path.Combine (dirLoc, "files", "file.csv");

The issue lies in how the path is being constructed. The Path.Combine method already handles path separators, so you don't need to add an extra leading backslash ().

var dirLoc = Path.GetDirectoryName(Assembly.GetEntryAssembly()!.Location);
var path = Path.Combine(dirLoc!, "files", "file.csv");

本文标签: cHow to get to the bin directoryStack Overflow