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

21,118

Solution 1

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

Solution 2

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);
}

Solution 3

More simple than the rest:

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

Solution 4

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?

Share:
21,118
Chilly Zhong
Author by

Chilly Zhong

I'm a mobile app developer. I was developing for Windows Mobile and Blackberry. I begin to dig into iPhone Development since Feb, 2009. Working has become a brand-new page for me and for my life. I was working on a symbian app for a short time in March, 2010. And I begin to learn Android in Nov, 2010 and learn WindowsPhone in Nov, 2011. Now I'm working on an iOS framework similar to iAd and I'm happy to offer all kinds of libs to help other developers. Always walking down your own way and keep your faith.

Updated on February 01, 2020

Comments

  • Chilly Zhong
    Chilly Zhong about 4 years

    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?

  • Chilly Zhong
    Chilly Zhong almost 15 years
    Thanks, but this will return the file's name "myApp" just like the method's name mentions. Thanks for the guide on this method anyway.
  • Chilly Zhong
    Chilly Zhong almost 15 years
    "Location" is not found in C# app, but everything in () can be replaced by the filepath I wrote and it works. Thanks.
  • JP Alioto
    JP Alioto almost 15 years
    Shucks! That's what I thought you wanted. :)
  • Chilly Zhong
    Chilly Zhong almost 15 years
    Thank you and danbyStrom, your guide works well. I'm trying to play sound using the wav file in my app's directory, so I need its directory(without app.exe).
  • Chilly Zhong
    Chilly Zhong almost 15 years
    ummm...it's simple but "Location" is not found(at least in Windows Mobile app).
  • Momoro
    Momoro almost 4 years
    Doesn't GetExecutingAssembly need ()? e.g. GetExecutingAssembly(), or is that just C#?