Click here to Skip to main content
11,482,402 members (74,488 online)
Click here to Skip to main content

Tagged as

Where Did My StartupPath Go?

, 16 Apr 2009 CPOL 15.4K 12
Rate this:
Please Sign up or sign in to vote.
Where did my StartupPath go?

So somebody has just asked where Application.StartupPath has disappeared in WPF. This is available in Windows Forms, but not available in WPF. A common technique to get the startup path is to use Assembly.GetEntryAssembly().Location, but there's a problem with GetEntryAssembly if your app is fired off from an unmanaged application, such as a COM application for instance.

We have a couple of applications where our executable is fired off as a result of unmanaged processing - primarily when processing image data that's been collected and checkpointed by some native code. We've found that GetEntryAssembly is null in this instance, whereas the following method works fine.

Now one way that you could add this in would be to add a reference to System.Windows.Forms into your project and then call it from there - however, there's a way that you can do it without needing to go anywhere near WinForms just by adding a few lines of code. Here it is - in all its glory.

public static class Utility
{
  [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
  public static extern int GetModuleFileName(HandleRef hModule, StringBuilder buffer,
      int length);

  private static string startupPath;
  private static HandleRef NullHandleRef = new HandleRef();

  public static string StartupPath()
  {
    if (startupPath == null)
    {
      StringBuilder buffer = new StringBuilder(260);
      GetModuleFileName(NullHandleRef, buffer, buffer.Capacity);
      startupPath = Path.GetDirectoryName(buffer.ToString());
    }
    new FileIOPermission(FileIOPermissionAccess.PathDiscovery, startupPath).Demand();
    return startupPath;
  }
}

As you can see, the code merely wraps up a call to GetModuleFileName, it's that simple.

Note - since posting this, one of my fellow WPF Disciples handed over the following code which should work as well:

startupPath = (Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly()).Location

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Pete O'Hanlon
CEO
United Kingdom United Kingdom
A developer for over 30 years, I've been lucky enough to write articles and applications for Code Project as well as the Intel Ultimate Coder - Going Perceptual challenge. I live in the North East of England with 2 wonderful daughters and a wonderful wife.

I am not the Stig, but I do wish I had Lotus Tuned Suspension.
Follow on   Twitter   Google+

Comments and Discussions

 
QuestionStringBuilder as a string? Pin
aspdotnetdev18-Aug-09 20:52
memberaspdotnetdev18-Aug-09 20:52 
AnswerRe: StringBuilder as a string? Pin
Luc Pattyn23-Mar-10 19:38
mvpLuc Pattyn23-Mar-10 19:38 
GeneralSurely this is the executable's path rather than startup path Pin
James H20-Apr-09 23:55
memberJames H20-Apr-09 23:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 16 Apr 2009
Article Copyright 2009 by Pete O'Hanlon
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid