Dan
Dan

Reputation: 129

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 e.g.

workingpath/myfolder

Can this be done?

Upvotes: 2

Views: 105

Answers (4)

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

Upvotes: 0

Neil
Neil

Reputation: 55402

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.

Upvotes: 0

Oded
Oded

Reputation: 499002

Just use the relative path to the application.

Upvotes: 1

Fosco
Fosco

Reputation: 38526

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/

Upvotes: 4

Related Questions