kyusan93
kyusan93

Reputation: 422

C# get directory name

I have a folder in c:\program files(x86)\company\application\ which contains all the app files.

How can I get the path like C:\program files(x86)\company?

I cannot use Application.Startuppath as it will return c:\program files)x86)\company\application.

Thanks.

Upvotes: 0

Views: 2418

Answers (3)

Writwick
Writwick

Reputation: 2163

See This MSDN Content about System.IO.Path.GetDirectoryName(string)

So, Include :

using System.IO;

Now Your Code:

string ApplicationPath = Application.StartupPath;
string YourPath = Path.GetDirectoryName(ApplicationPath);
// This will get "C:\program files(x86)\company" from "C:\program files(x86)\company\application"

Upvotes: 0

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48580

Use Directory.GetParent(string path)

In your app include System.IO namespace

using System.IO;

and use

string desired_path = Directory.GetParent(Application.StartupPath);
                       // will return c:\program files(x86)\company
        // because app path will have c:\program files(x86)\company\application\

Upvotes: 3

Shailesh
Shailesh

Reputation: 1218

Best to use Environment.CurrentDirectory and it will help you.

Please refer http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory%28v=vs.71%29.aspx

for more details.

Upvotes: 0

Related Questions