[英]How to specify non exact paths in C#
I currently have a statement as follows:我目前有如下声明:
string dir = "C:\\Users\\Limited\\Desktop\\";
Although I would like it to be specified as a directory within the work directroy eg尽管我希望将其指定为工作目录中的目录,例如
workingpath/myfolder工作路径/我的文件夹
Can this be done?这可以做到吗?
I assumed you could just use a relative path, ie "myfolder"
, but you can get and use the application path and append the subdirectory:我假设您可以只使用相对路径,即"myfolder"
,但您可以获取并使用应用程序路径和 append 子目录:
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
http://www.csharp-examples.net/get-application-directory/ http://www.csharp-examples.net/get-application-directory/
Just use the relative path to the application.只需使用应用程序的相对路径即可。
Unless your path begins with a (drive letter or back)slash¹, it is interpreted as relative to the current working directory.除非您的路径以(驱动器号或反)斜杠¹开头,否则它将被解释为相对于当前工作目录。 So "myfolder\\"
would be a relative directory.所以"myfolder\\"
将是一个相对目录。
¹In MS-DOS, and emulated by cmd.exe, it's possible to have a path relative to the current directory on another drive. ¹在 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.