Chilly Zhong
Chilly Zhong

Reputation: 16783

How to get the path of app(without app.exe)?

I want to get the path of my app like: "\\ProgramFiles\\myApp", I try to use the following code:


string path = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;

But it returns a path which has "\\myapp.exe" at the end.

I also tried:


string path = System.IO.Directory.GetCurrentDirectory();

But it throws an “NotSupportedException”.

Is there any way to get a path without .exe at the end?

Upvotes: 8

Views: 22643

Answers (7)

amrtn
amrtn

Reputation: 597

What about using a FileInfo object to extract the directory name?

In Vb.Net:

fi = New FileInfo(System.Reflection.Assembly.GetExecutingAssembly.Location)
path = fi.DirectoryName

Upvotes: 0

Dan Byström
Dan Byström

Reputation: 9244

path = System.IO.Path.GetDirectoryName( path );

Upvotes: 12

Fredrik Mörk
Fredrik Mörk

Reputation: 158349

Application.StartupPath should do that for you.

Update: from you edit I see that you are running on Compact Framework. Then Application.StartupPath will not work. This is the construct that I usually use then:

private static string GetApplicationPath()
{
    return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}

Upvotes: 19

Chris
Chris

Reputation: 28064

More simple than the rest:

using System.IO;
using System.Reflection;
...
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)

Upvotes: 3

NileshChauhan
NileshChauhan

Reputation: 5569

If its an exe as in your case use:

    // Summary:
    //     Gets the path for the executable file that started the 
    //     application, not including the executable name.    
    Application.StartupPath

Upvotes: 0

JP Alioto
JP Alioto

Reputation: 45127

Path.GetFileNameWithoutExtension(path);

Upvotes: 0

okutane
okutane

Reputation: 14260

You can use Path.GetDirectoryName(string) method passing your original path string as parameter for this. What problem are you trying to solve? Maybe you really need something like working directory?

Upvotes: 1

Related Questions