繁体   English   中英

如何在 C# 中指定非精确路径

[英]How to specify non exact paths in C#

我目前有如下声明:

string dir = "C:\\Users\\Limited\\Desktop\\";

尽管我希望将其指定为工作目录中的目录,例如

工作路径/我的文件夹

这可以做到吗?

我假设您可以只使用相对路径,即"myfolder" ,但您可以获取并使用应用程序路径和 append 子目录:

string appPath = Path.GetDirectoryName(Application.ExecutablePath);

http://www.csharp-examples.net/get-application-directory/

只需使用应用程序的相对路径即可。

除非您的路径以(驱动器号或反)斜杠¹开头,否则它将被解释为相对于当前工作目录。 所以"myfolder\\"将是一个相对目录。

¹在 MS-DOS 中,并由 cmd.exe 模拟,可能具有相对于另一个驱动器上的当前目录的路径。

const string subDir = "test_dir";
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
string targetPath = Path.Combine(appPath, subDir);

暂无
暂无

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

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