June 18, 2010

How to get the current running executable name as a string in C#?

Filed under: C# (C-Sharp) — J. Abram barneck @ 6:00 am

Ok, there are multiple options.

Here is the code, you choose the option you want.

It is best to use Option 1 or Option 2. Read this blog at MSDN for a better understanding:
Assembly.CodeBase vs. Assembly.Location

using System;

namespace GetCurrentProcessName
    class Program
        static void Main(string[] args)
            // It is best to use Option 1 or Option 2.  Read this:
            // http://blogs.msdn.com/b/suzcook/archive/2003/06/26/assembly-codebase-vs-assembly-location.aspx

            // Option 1 - Using Location (Recommended)
            String fullExeNameAndPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            String ExeName = System.IO.Path.GetFileName(fullExeNameAndPath);

            // Option 2 - Using CodeBase instead of location
            String fullExeNameAndPathUrl = System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
            String codeBase = System.IO.Path.GetFileName(fullExeNameAndPathUrl);

            // Option 3 - Usable but during debugging in Visual Studio it retuns ExeName.vhost.exe
            String fullVhostNameAndPath = Environment.GetCommandLineArgs()[0];
            String vhostName = System.IO.Path.GetFileName(fullVhostNameAndPath);

            // Option 4 - Also usable but doesn't include the extension .exe and also returns ExeName.vhost
            // during debuggin in visual studio
            String prcessName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;

Copyright ® Rhyous.com – Linking to this page is allowed without permission and as many as ten lines of this page can be used along with this link. Any other use of this page is allowed only by permission of Rhyous.com.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at WordPress.com.

%d bloggers like this: