开发者

How to specify non exact paths in C#

开发者 https://www.devze.com 2023-03-11 10:07 出处:网络
I currently have a statement as follows: string dir = \"C:\\\\Users\\\\Limited\\\\Desktop\\\\\"; Although I would like it to be specified as a direc开发者_StackOverflow社区tory within the work dire

I currently have a statement as follows:

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

Although I would like it to be specified as a direc开发者_StackOverflow社区tory within the work directroy e.g.

workingpath/myfolder

Can this be done?


I assumed you could just use a relative path, i.e. "myfolder", but you can get and use the application path and append the subdirectory:

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

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.

¹In MS-DOS, and emulated by cmd.exe, it's possible to have a path relative to the current directory on another drive.


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

精彩评论

暂无评论...
验证码 换一张
取 消