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 |2 Answers
Reset to default 0When 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
版权声明:本文标题:c# - How to get to the bin directory - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742048246a2417920.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Path.Combine(dirloc, "files\\file.csv")
. – Richard Commented Jan 21 at 10:57Path.Combine(dirloc, "files", "file.csv")
. One feature ofPath
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