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

被刻印的时光 ゝ 提交于 2019-12-03 04:50:36

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);

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.

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

Try this to get the version:

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

And this to get the name:

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

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.

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).

This would also get you name / version

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

try

var asm = Assembly.GetExecutingAssembly();

Teja

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!