RSS

Get Application Directory in C#

Sun, Sep 7, 2008

Programming, Tutorials

This example demonstrates how can we get application or assembly folder.

Geting Directory of the windows forms application

To get directory we need to use static property ExecutablePath from Application in System.Window­s.Forms namespace.

By using static method GetDirectoryName of Path class we can get only the folder part of the path.

string appPath = Path.GetDirectoryName(Application.ExecutablePath);

Getting Directory of loaded assembly

By using the method Assembly.GetE­xecutingAssem­bly we can get assembly of currently executing program.
By using method Assembly.GetAs­sembly we can get assembly in which the specified class is defined. Finally, we can get a assembly file path using Assembly.CodeBase property.

string appPath = Path.GetDirectoryName(
                     Assembly.GetAssembly(typeof(MyClassName)).CodeBase);
Sharing ~ Helping Other:
  • Print
  • email
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • BlinkList
  • DZone
  • Slashdot
  • YahooMyWeb
  • StumbleUpon
  • Live
  • IndianPad
  • DotNetKicks
  • Technorati

Related Posts:

, ,

This post was written by:

eXclusiveMinds - who has written 500 posts on eXclusiveMinds.


Contact the author

Leave a Reply

You must be logged in to post a comment.