2

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!

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180
  • To what end will you use this information? If this is an intermediate step on a solution you've already devised, we may do better knowing what the overall goal is... – Damien_The_Unbeliever Nov 16 '11 at 19:07
  • possible duplicate of [how to get the application physical path in windows forms application](http://stackoverflow.com/questions/5788881/how-to-get-the-application-physical-path-in-windows-forms-application) – Rowland Shaw Nov 16 '11 at 19:09

6 Answers6

7
 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)
Glory Raj
  • 17,397
  • 27
  • 100
  • 203
3
System.Reflection.Assembly.GetEntryAssembly().Location
Russell McClure
  • 4,821
  • 1
  • 22
  • 22
2

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

Ville Krumlinde
  • 7,021
  • 1
  • 33
  • 41
1

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.

KeithS
  • 70,210
  • 21
  • 112
  • 164
0

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.

Hogan
  • 69,564
  • 10
  • 76
  • 117
0

You can use:

System.Reflection.Assembly.GetExecutingAssembly().Location
competent_tech
  • 44,465
  • 11
  • 90
  • 113