简体   繁体   中英

In C# how do i get the name of the running file?

I would like to know how to pragmatically get the name of the running file not the assembly name but the name of the file in C# .NET,

I tried

System.Reflection.Assembly.GetEntryAssembly().GetName().Name

this gives the name of the assembly but i am looking for the name of the exe file instead.

Thank you!

 System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

or

If you want the executable:

System.Reflection.Assembly.GetEntryAssembly().Location

If you'd like just the file*name* and not the path, use:

Path.GetFileName(System.Reflection.Assembly.GetEntryAssembly().Location)
System.Reflection.Assembly.GetEntryAssembly().Location

System.Windows.Forms.Application.ExecutablePath

"Gets the path for the executable file that started the application, including the executable name."

http://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx

If you know your assembly is the EXE in which you are running (which I infer is likely, by the use of "GetEntryAssembly()"), you can use Process.GetCurrentProcess().ProcessName. This will return the name of the EXE for your program.

http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs.aspx

Look at the first element in this array.

 System.Environment.GetCommandLineArgs()[0];

You can also look at args[0] in main(string args[])

This should change even if the program is renamed.

您可以使用:

System.Reflection.Assembly.GetExecutingAssembly().Location

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM