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

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.

One Comment

  1. [...] running executable name as a string in C#? Filed under: FreeBSD — rhyous @ 9:32 pm Read more Share this:DiggRedditLike this:LikeBe the first to like this post. Leave a [...]

Leave a Reply

How to post code in comments?