I want to get the name of the currently running program, that is the executable name of the program. In C/C++ you get it from args[0]
.
-
Executable is EXE file (Windows Forms, WPF applications) ? A program can be a Desktop App (WinForms, WPF; and WinRT-Windows Phone?), Web Application, Wcf Service Application, Visual Studio Addin, Outlook-Word Addin, Unit Test in VS (MSTest), or, Silverlight Application.– KiquenetMar 27, 2014 at 14:36
21 Answers
System.AppDomain.CurrentDomain.FriendlyName
-
73Beware of accepted answer. We've had issues with using
System.AppDomain.CurrentDomain.FriendlyName
under Click-Once deployed applications. For us, this is returning "DefaultDomain", and not the original exe name.– GaspodeApr 13, 2010 at 14:30 -
47We used this in the end:
string file = object_of_type_in_application_assembly.GetType().Assembly.Location; string app = System.IO.Path.GetFileNameWithoutExtension( file );
– GaspodeMay 10, 2011 at 15:10 -
6FriendlyName can be set to anything. Also getting the assembly location may not be enough if you have an exe with several dlls. Furthermore if you use several AppDomain, Assembly.GetCallingAssembly() returns null. May 16, 2012 at 1:19
-
3@Gaspode : it would be easier just to say Path.GetFileNameWithoutExtension(GetType().Assembly.Location) - you don't need to specify an object of a type in the current assembly. You can use GetType of this, and then you don't even need to say "this." Sep 24, 2012 at 19:36
-
9This can be useful, but it should not be the accepted answer: It is vastly different from what was asked for - it will coincidentally be the same thing in some situations, but this is something else entirely. If you didn't write the application yourself it could very well return "I like potatoes!" or whatever else your humorous colleague wrote into this property when they built the application! Jun 12, 2016 at 1:52
System.AppDomain.CurrentDomain.FriendlyName
- Returns the filename with extension (e.g. MyApp.exe).
System.Diagnostics.Process.GetCurrentProcess().ProcessName
- Returns the filename without extension (e.g. MyApp).
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
- Returns the full path and filename (e.g. C:\Examples\Processes\MyApp.exe). You could then pass this into System.IO.Path.GetFileName()
or System.IO.Path.GetFileNameWithoutExtension()
to achieve the same results as the above.
-
5AppDomain can be a EXE application, Web application, Unit test application, Addin Visual Studio, and "Silverlight App"(?). Maybe interesting full solution for all cases. For example, for Unit Test VS2012 - ProcessName: vstest.executionengine.x86 MainModule.FileName: C:\PROGRAM FILES (X86)\MICROSOFT VISUAL STUDIO 11.0\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\TESTWINDOW\vstest.executionengine.x86.exe MainModule.ModuleName: vstest.executionengine.x86.exe FriendlyName: UnitTestAdapter: Running test ApplicationName:– KiquenetMar 26, 2014 at 8:18
-
A "program" can be a Desktop App (WinForms, WPF; and WinRT-Windows Phone?), Web Application, Wcf Service Application, Visual Studio Addin, Outlook-Word Addin, Unit Test in VS (MSTest), or, Silverlight Application. For example, how get service Host assembly for a Wcf Service Application hosted in IIS, not IISExpress or WebDevServer ?– KiquenetMar 27, 2014 at 14:37
-
6+1 I’m going to go with this answer because it provides all three variations that you might need in a clean and simple way. Using the bare program name without path or extension is very useful for in-program help text (
/?
switch), because using the extension and path just clutter it unnecessarily.– SynetechJul 6, 2014 at 19:20 -
3
-
1Under Mono on Linux,
Process.GetCurrentProcess().MainModule.FileName
returned/usr/bin/mono-sgen
. TheGetType().Assembly.Location
approach returned the desired executable path for both Linux and Windows.– faddenOct 1, 2018 at 17:20
This should suffice:
Environment.GetCommandLineArgs()[0];
-
3Hmm, this returns (when run from vs.net and using the debug hosting thing), the location and name of the filename.vshost.exe ... that is indeed the file that is executing at this time ) Mar 5, 2009 at 21:05
-
16This the best answer for me because
Environment.GetCommandLineArgs()
is the exact C# analogue ofargv
from C/C++. Jun 3, 2012 at 6:45 -
Agreed! best answer. I have a need to get Environment.GetCommandLineArgs()[1]; Mar 16, 2013 at 4:02
-
5To avoid full path:
Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0])
– NathanApr 24, 2014 at 0:34 -
2This works well when trying to track down WCF services. The process name comes back with iisexpress in my case. But this command gives me the actual WCF service assembly name. Mar 1, 2018 at 16:53
System.Diagnostics.Process.GetCurrentProcess()
gets the currently running process. You can use the ProcessName
property to figure out the name. Below is a sample console app.
using System;
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Process.GetCurrentProcess().ProcessName);
Console.ReadLine();
}
}
-
40
-
Process.GetCurrentProcess().MainModule.FileName works perfectly from within an Excel Addin (ExcelDNA)– earcamMar 15, 2012 at 10:24
-
11This approach will fail when used on the Mono runtime; the process name for applications running on Mono will always be some variant of
.../bin/mono
on *nixes or.../mono.exe
on Windows.– cdhowieOct 21, 2012 at 20:11 -
1This should be the accepted answer. Current AppDomain name may have nothing to do with executable process name, especially when multiple app domains are present Aug 12, 2015 at 19:47
-
1@Frank Perhaps using OS-specific tricks, such as reading
/proc/self/cmdline
. (var args = File.ReadAllText("/proc/self/cmdline").Split('\0'); args = args.Take(args.Length - 1).ToArray();
would give you an array of such arguments.)– cdhowieFeb 17, 2016 at 6:10
This is the code which worked for me:
string fullName = Assembly.GetEntryAssembly().Location;
string myName = Path.GetFileNameWithoutExtension(fullName);
All the examples above gave me the processName with vshost or the running dll name.
-
5For those that dont know, or missed it in other answers, the namespace for Assembly is System.Reflection, and the namespace for Path is System.IO. Apr 2, 2015 at 14:19
-
5GetEntryAssembly will return null if the application entry point is in native code rather than an assembly. Jul 4, 2015 at 23:34
-
2
Try this:
System.Reflection.Assembly.GetExecutingAssembly()
This returns you a System.Reflection.Assembly
instance that has all the data you could ever want to know about the current application. I think that the Location
property might get what you are after specifically.
-
7It might be safer to use
CodeBase
instead ofLocation
in case .NET's shadow copy feature is active. See blogs.msdn.com/suzcook/archive/2003/06/26/… Aug 12, 2009 at 9:28 -
23Beware of GetExecutingAssembly(): if you call this from a library assembly, it returns the name of the library assembly, which is different from the name of the entry assembly (i.e. the original executable). If you use GetEntryAssembly(), it returns the name of the actual executable, but it throws an exception if the process is running under WCF (admittedly a rare situation). For the most robust code, use Process.GetCurrentProcess().ProcessName.– ContangoNov 3, 2010 at 12:01
-
@Gravitas: Certainly not, any executable that is running "interpreted", e.g. with /usr/bin/mono will have the wrong process name. Also ProcessName won't work with windows services. If you use it in a library, use GetCallingAssembly. Mar 6, 2013 at 20:15
-
2Worked for me. The property Name of the returned Assembly instance GetName() call is what you need, and does not include the ".exe" part. Also tested on Mono/Linux with expected result. Assembly.GetName().Name Jan 22, 2014 at 23:54
-
1Hmm, note that the returned string won't change even if you rename the executable file by hand using the file explorer. While Environment.GetCommandLineArgs()[0] changes along with the actual executable filename (of course). Coincidentally, the second method resulted better for my specific situation as I want the data folder to be named as the actual executable filename. Jan 23, 2014 at 2:25
Why nobody suggested this, its simple.
Path.GetFileName(Application.ExecutablePath)
-
3
-
9
-
@NineBerry You might be interested in
Application.ExecutablePath
's source code.– SpookyJan 21, 2016 at 23:03 -
@NineBerry See my post. This works within Console apps if you add a reference to System.Windows.Forms.– JohnJul 15, 2017 at 0:13
Couple more options:
System.Reflection.Assembly.GetExecutingAssembly().GetName().Name
Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase
System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.Name;
will give you FileName of your app like; "MyApplication.exe"
If you need the Program name to set up a firewall rule, use:
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
This will ensure that the name is correct both when debugging in VisualStudio and when running the app directly in windows.
-
2For my purposes (creating a logging filename), this is the best answer. If running a hosted process (say, a service, or a web application), System.AppDomain.CurrentDomain.FriendlyName can return an ugly GUID-y name with embedded slashes.– CurtAug 8, 2016 at 17:59
-
This does not work for .NET Core 2+, which will report "dotnet.exe" - or perhaps the apphost if setup as such. Jul 8, 2020 at 1:52
When uncertain or in doubt, run in circles, scream and shout.
class Ourself
{
public static string OurFileName() {
System.Reflection.Assembly _objParentAssembly;
if (System.Reflection.Assembly.GetEntryAssembly() == null)
_objParentAssembly = System.Reflection.Assembly.GetCallingAssembly();
else
_objParentAssembly = System.Reflection.Assembly.GetEntryAssembly();
if (_objParentAssembly.CodeBase.StartsWith("http://"))
throw new System.IO.IOException("Deployed from URL");
if (System.IO.File.Exists(_objParentAssembly.Location))
return _objParentAssembly.Location;
if (System.IO.File.Exists(System.AppDomain.CurrentDomain.BaseDirectory + System.AppDomain.CurrentDomain.FriendlyName))
return System.AppDomain.CurrentDomain.BaseDirectory + System.AppDomain.CurrentDomain.FriendlyName;
if (System.IO.File.Exists(System.Reflection.Assembly.GetExecutingAssembly().Location))
return System.Reflection.Assembly.GetExecutingAssembly().Location;
throw new System.IO.IOException("Assembly not found");
}
}
I can't claim to have tested each option, but it doesn't do anything stupid like returning the vhost during debugging sessions.
-
3+1 for amusement. :-) I would hardly use this code, though, unless I'm writing a really generic library that has no idea about its environment (and then it probably wouldn't be a good idea to maintain whatever global state you were going to use the name for). Mar 22, 2013 at 8:57
-
@Orwellophile A "program" can be a Desktop App (WinForms, WPF; and WinRT-Windows Phone?), Web Application, Wcf Service Application, Visual Studio Addin, Outlook-Word Addin, Unit Test in VS (MSTest), or, Silverlight Application. For example, how get service Host assembly for a Wcf Service Application hosted in IIS, not IISExpress or WebDevServer ? Any full code valid for A WinForms, WPF, Web Application, Wcf Service Application, Visual Studio Addin, Outlook-Word Addin, Unit Test in VS (MSTest) applications ?– KiquenetMar 27, 2014 at 14:39
System.Reflection.Assembly.GetEntryAssembly().Location
returns location of exe name if assembly is not loaded from memory.System.Reflection.Assembly.GetEntryAssembly().CodeBase
returns location as URL.
-
Tested, this works 100%, even if its called from within a C# library.– ContangoNov 3, 2010 at 10:58
-
1GetEntryAssembly() returns null if you're not in the main AppDomain. May 16, 2012 at 1:20
This works if you need only the application name without extension:
Path.GetFileNameWithoutExtension(AppDomain.CurrentDomain.FriendlyName);
You can use Environment.GetCommandLineArgs()
to obtain the arguments and Environment.CommandLine
to obtain the actual command line as entered.
Also, you can use Assembly.GetEntryAssembly()
or Process.GetCurrentProcess()
.
However, when debugging, you should be careful as this final example may give your debugger's executable name (depending on how you attach the debugger) rather than your executable, as may the other examples.
-
4Beware of GetExecutingAssembly(): if you call this from a library assembly, it returns the name of the library assembly, which is different from the name of the entry assembly (i.e. the original executable). If you use GetEntryAssembly(), it returns the name of the actual executable, but it throws an exception if the process is running under WCF (admittedly a rare situation). For the most robust code, use Process.GetCurrentProcess().ProcessName.– ContangoNov 3, 2010 at 12:02
-
@Gravitas: good point - wow, it's been a while since I wrote this! :D I'll edit accordingly Nov 3, 2010 at 14:17
-
Environment.CommandLine
gives the absolute path, not the entered command line, at least on Mono/Linux. Dec 9, 2012 at 0:14 -
@Mechanicalsnail: Sounds like Mono doesn't quite follow the documentation. Interesting. Dec 10, 2012 at 15:29
IF you are looking for the full path information of your executable, the reliable way to do it is to use the following:
var executable = System.Diagnostics.Process.GetCurrentProcess().MainModule
.FileName.Replace(".vshost", "");
This eliminates any issues with intermediary dlls, vshost, etc.
-
I tried your reliable way in Ubuntu Linux 15.10 C++ using realpath followed by STL C++ string replace and it caused Point and Click to fail. Could that have been caused by a bug in mono as our director of software surmised today? Thanks.– FrankFeb 17, 2016 at 3:35
-
-
Gasponde wrote above that "We've had issues with using System.AppDomain.CurrentDomain.FriendlyName under Click-Once deployed applications." Could you make a guess as to what the issues might be with Click-Once deployed applications in .NET? Thanks.– FrankFeb 17, 2016 at 5:42
-
1Returns C:\Program Files\dotnet\dotnet.exe for my Sample program in VS2017. Sep 2, 2019 at 21:23
If you are publishing a single file application in .NET 6.0 or above, you can use Environment.ProcessPath
Is this what you want:
Assembly.GetExecutingAssembly ().Location
-
4Beware of GetExecutingAssembly(): if you call this from a library assembly, it returns the name of the library assembly, which is different from the name of the entry assembly (i.e. the original executable). If you use GetEntryAssembly(), it returns the name of the actual executable, but it throws an exception if the process is running under WCF (admittedly a rare situation). For the most robust code, use Process.GetCurrentProcess().ProcessName.– ContangoNov 3, 2010 at 12:02
-
On .Net Core (or Mono), most of the answers won't apply when the binary defining the process is the runtime binary of Mono or .Net Core (dotnet) and not your actual application you're interested in. In that case, use this:
var myName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetEntryAssembly().Location);
-
1
For windows apps (forms and console) I use this:
Add a reference to System.Windows.Forms in VS then:
using System.Windows.Forms;
namespace whatever
{
class Program
{
static string ApplicationName = Application.ProductName.ToString();
static void Main(string[] args)
{
........
}
}
}
This works correctly for me whether I am running the actual executable or debugging within VS.
Note that it returns the application name without the extension.
John
Super easy, here:
Environment.CurrentDirectory + "\\" + Process.GetCurrentProcess().ProcessName
-
1For .NET Core Process.GetCurrentProcess().ProcessName returns "dotnet". Aug 29, 2018 at 23:40
-
1The current directory is temporally transient and cannot be relied on to be the location of the assembly/executable. Sep 2, 2019 at 20:48
To get the path and the name
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName