How do I get the .exe name of a C# console application?

28.7k views Asked by At

I'm debugging "xiixtasks.exe", a C# console-mode application in VS2008.

I'm trying to get the version info from xiixtasks.exe.

When I try "Process.GetCurrentProcess()", it gives me the filename and version info for vshost.exe, NOT xiixtasks.exe:

  // WRONG: this gives me xiixtasks.vhost.exe, version 9.0.30729.1
  //        I *want* "xiixtasks.exe", version 1.0.0.1024
  System.Diagnostics.FileVersionInfo fi =
    System.Diagnostics.Process.GetCurrentProcess().MainModule.FileVersionInfo;

What should I be doing instead?

Thank you in advance!

======================================================

Solution:

1) The initial problem was indeed the IDE's "vshost" wrapper. One workaround would have been to change the build settings.

2) Assembly.GetExecutingAssembly().CodeBase is an excellent solution - thank you!. It works inside and outside the debugger.

3) Unfortunately, when I tried calling it with a function that expected a normal file path (instead of a URI like GetExecutingAssembly()" gives you), it died with a "Uri formats are not supported" exception.

4) Final solution: call GetExecutingAssembly(), then Uri.LocalPath ():

...
else if (cmdArgs.cmd.Equals(CmdOptions.CMD_SHOW_VERSION))
{
    string codeBaseUri = 
       Urifile.System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
    string codeBase =
        new Uri (codeBaseUri).LocalPath;
    string sVersion = Util.GetWindowsVersion(codeBase);
    System.Console.WriteLine ("version({0}): {1}: ",
        Util.Basename(codeBase), sVersion);
}

Thank you once again, all!

11

There are 11 answers

2
as-cii On BEST ANSWER

Full Path of your assembly:

Assembly.GetExecutingAssembly().CodeBase.Dump();

You can always extract the name with Path.GetFileName:

string codeBase = Assembly.GetExecutingAssembly().CodeBase;
string name = Path.GetFileName(codeBase);
0
Teja On

If you will get the exception like "URI format is not supported" While Copying the xml file from one directory to the another by using the following code.

string path=System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
DirectoryInfo dir=new DirectoryInfo(path+"\\App2");
FileInfo[] Files=dir.GetFiles();

then convert it into the URI like.

string path =Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); 
URI uri = new URI(path); 
string FileName = Path.Combine(uri.LocalPath, "\\App2"+file.Name);

then use it to get the files.

0
AudioBubble On

Try this to get the version:

System.Reflection.Assembly.GetExecutingAssembly().GetName().Version

And this to get the name:

System.Reflection.Assembly.GetExecutingAssembly().GetName().ToString()
2
Marc Gravell On

It sounds like you are running inside the IDE in debug with the "Enable the Visual Studio hosting process" checkbox enabled. In which case, the current process is xiixtasks.vshost.exe - it is a shell exe used to help debugging. You need to disable that checkbox.

enter image description here

0
Chris Stavropoulos On

That's actually what I would expect as it is in fact executing the vshost file in debug mode.

If you don't want it to execute the vshost file but rather your exe directly, you need to go into your project settings and disable the vshost debugging option.

1
Saeed Amiri On

try

var asm = Assembly.GetExecutingAssembly();

0
Ken Beckett On

The 'vshost.exe' wrapper is generally only enabled for Debug builds, and it's also not necessary to use it, so perhaps you should just consider turning it off under your project settings (Debug tab, uncheck 'Enable the Visual Studio hosting process' at the bottom).

0
KreepN On

Depending on what info you want, you can use a variety of calls:

enter image description here

0
jav On

What worked for me is:

using System;
namespace DemoApplication{
   public class Program{
      public static void Main(){
         string currentExecutable =
         System.AppDomain.CurrentDomain.FriendlyName;
         Console.WriteLine($"Current Executable Name: {currentExecutable}");
         Console.ReadLine();
      }
   }
}

Link: https://www.tutorialspoint.com/how-to-get-the-name-of-the-current-executable-in-chash

0
sɐunıɔןɐqɐp On

For .NET 6 Console App (or later)

    Assembly.GetExecutingAssembly().Location

will give you the full path of the executing assembly DLL.

To get the executable name you can replace the extension:

string dllPath = Assembly.GetExecutingAssembly().Location;
string exePath = $"{Path.GetFileNameWithoutExtension(dllPath)}.exe";
FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(exePath);

References

0
ondrovic On

This would also get you name / version

name: Assembly.GetExecutingAssembly().GetName().Name
version: = Assembly.GetExecutingAssembly().GetName().Version