简体   繁体   English

在c#中获取被调用可执行文件的完整路径和文件名的最佳方法是什么?

[英]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%准确的:

  • if I call it as commandline.exe foo bar baz from the same directory; 如果我从同一目录将其称为commandline.exe foo bar baz all is well 一切都很好
  • if I call it as commandline foo bar baz from the same directory, I only get the filename sans extension; 如果我从同一目录将其称为commandline foo bar baz ,则只会得到没有扩展名的文件名; not what I want 不是我想要的
  • if I call it as 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.

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