Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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!

like image 311
paulsm4 Avatar asked Oct 24 '11 19:10

paulsm4


People also ask

What is the Command that specifies the name of your executable file that you are going to build?

The /Fe option allows you to specify the output directory, output executable name, or both, for the generated executable file.

What is process start in c#?

Start(String) Starts a process resource by specifying the name of a document or application file and associates the resource with a new Process component. public: static System::Diagnostics::Process ^ Start(System::String ^ fileName); C# Copy.

How do I close an app in C#?

Exit a Console Application With the Application. Exit() Function in C# The Application. Exit() function terminates all the message loops started with the Application.


2 Answers

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); 
like image 155
as-cii Avatar answered Sep 24 '22 09:09

as-cii


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

like image 24
Marc Gravell Avatar answered Sep 20 '22 09:09

Marc Gravell