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]
.
System.AppDomain.CurrentDomain.FriendlyName
System.AppDomain.CurrentDomain.FriendlyName
under Click-Once deployed applications. For us, this is returning "DefaultDomain", and not the original exe name. –
Paranoiac string file = object_of_type_in_application_assembly.GetType().Assembly.Location; string app = System.IO.Path.GetFileNameWithoutExtension( file );
–
Paranoiac 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.
/?
switch), because using the extension and path just clutter it unnecessarily. –
Stretchy GetCurrentProcess()
–
Dyane Process.GetCurrentProcess().ProcessName()
returns MyApp.vshost for me. –
Feverous Process.GetCurrentProcess().MainModule.FileName
returned /usr/bin/mono-sgen
. The GetType().Assembly.Location
approach returned the desired executable path for both Linux and Windows. –
Clementineclementis This should suffice:
Environment.GetCommandLineArgs()[0];
Environment.GetCommandLineArgs()
is the exact C# analogue of argv
from C/C++. –
Kuhlman Path.GetFileNameWithoutExtension(Environment.GetCommandLineArgs()[0])
–
Polinski System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
–
Iverson Environment.ProcessPath
(see: learn.microsoft.com/en-us/dotnet/api/… ) –
Melpomene 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();
}
}
.../bin/mono
on *nixes or .../mono.exe
on Windows. –
Defaulter /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.) –
Defaulter Environment.CommandLine
should be the invocation string. Could be that mono filters itself out from it - or not - I haven't tested it on a linux machine myself so you would have to test it yourself. (I think it does filter away the mono-part since some applications could have behavior that depends on this property - and they probably wont be expecting it to be prefixed by mono "stuff".) –
Flume 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.
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.
CodeBase
instead of Location
in case .NET's shadow copy feature is active. See blogs.msdn.com/suzcook/archive/2003/06/26/… –
Kor Why nobody suggested this, its simple.
Path.GetFileName(Application.ExecutablePath)
Application.ExecutablePath
's source code. –
Samovar 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.
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.
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.
If you are publishing a single file application in .NET 6.0 or above, you can use Environment.ProcessPath
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.
Environment.CommandLine
gives the absolute path, not the entered command line, at least on Mono/Linux. –
Doersten 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.
Is this what you want:
Assembly.GetExecutingAssembly ().Location
Super easy, here:
Environment.CurrentDirectory + "\\" + Process.GetCurrentProcess().ProcessName
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);
GetEntryAssembly()
can return null. –
Chobot 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
To get the path and the name
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
© 2022 - 2024 — McMap. All rights reserved.