[英]What's the best way to get the full path and filename of the invoked executable in c#?
Given the following code: 给出以下代码:
using System;
namespace Sandbox
{
class CommandLine
{
static void Main()
{
String[] args = Environment.GetCommandLineArgs();
String executable = args[0];
String[] paramList = getParamList(args);
System.Console.WriteLine("Directory ....... {0}", Environment.CurrentDirectory);
System.Console.WriteLine("Executable ...... {0}", args[0]);
System.Console.WriteLine("Params .......... {0}", String.Join(", ", paramList));
}
private static String[] getParamList(String[] args)
{
String[] paramList = new String[args.Length - 1];
for (int i = 1; i < args.Length; i++)
{
int j = i - 1;
paramList[j] = args[i];
}
return paramList;
}
}
}
... Saved as commandline.cs and csc
'd to commandline.exe ...另存为commandline.cs和
csc
到commandline.exe
I'd like to get the full path and filename of the execuatable being invoked. 我想获取被调用的可执行文件的完整路径和文件名。 This code almost does it, however it's not 100% accurate:
这段代码几乎可以做到,但是它不是100%准确的:
commandline.exe foo bar baz
from the same directory; commandline.exe foo bar baz
; all is well commandline foo bar baz
from the same directory, I only get the filename sans extension; commandline foo bar baz
,则只会得到没有扩展名的文件名; not what I want sandbox\\commandline foo bar baz
from the parent directory; sandbox\\commandline foo bar baz
; I get the relative path and partial filename I'm sure there's a much easier way of getting the full path and filename of the executable other than string manipulation, right? 我敢肯定有获得比字符串操作可执行其它的完整路径和文件名,右的更简单的方法?
Application.ExecutablePath . Application.ExecutablePath 。
Note that this requires a reference to system.windows.forms
. 请注意,这需要对
system.windows.forms
的引用。
That will be 那将是
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
Be aware, though, that when running your application from within Visual Studio, this will most likely return full path to a vshost file . 但是请注意,从Visual Studio中运行应用程序时,这很可能返回vshost文件的完整路径。
使用Assembly.GetExecutingAssembly().CodeBase
This is what we use. 这就是我们使用的。 Seems to work from Console, WinForms, and WPF apps running from Visual Studio or standalone.
似乎可以从Visual Studio或独立运行的Console,WinForms和WPF应用程序中工作。
System.Reflection.Assembly entryAssembly =
System.Reflection.Assembly.GetEntryAssembly();
string applicationPath = entryAssembly != null ? entryAssembly.Location :
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
string applicationDirectory = Path.GetDirectoryName(applicationPath);
Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
string path = System.Windows.Forms.Application.ExecutablePath;
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
Application.ExecutablePath will give you path of the application that executed your code. Application.ExecutablePath将为您提供执行代码的应用程序的路径。 For instance app that called your dll.
例如调用您的dll的应用程序。 The line above provides you full path to your app.
上面的行为您提供了应用程序的完整路径。
You can read more at: HOW TO: Determine the Executing Application's Path 您可以在以下位置阅读更多信息: 如何:确定执行应用程序的路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.