How do I get Bin Path?

use*_*969 69 .net c#

I need to the the bin path of the executing assembly. How do you get it? I have a folder Plugins in the Bin/Debug and I need to get the location

kem*_*002 105

以下是获取应用程序执行路径的方法:

var path = System.IO.Path.GetDirectoryName( 
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
Run Code Online (Sandbox Code Playgroud)

MSDN有关于如何确定执行应用程序路径的完整参考.

请注意,值path将采用以下形式file:\c:\path\to\bin\folder,因此在使用路径之前,您可能需要将其file:\从前面剥离.例如:

path = path.Substring(6);
Run Code Online (Sandbox Code Playgroud)

  • 在我的例子中,问题是System.IO.Path.GetDirectoryName()在开头返回一个带有"file:\\"的路径.我刚从字符串中删除它,一切正常. (6认同)
  • 我建议使用.Replace("file:\\","")来删除文件前缀,因为它更具体地解决了问题. (5认同)

eto*_*bot 61

你可以做到这一点

    Assembly asm = Assembly.GetExecutingAssembly();
    string path = System.IO.Path.GetDirectoryName(asm.Location);
Run Code Online (Sandbox Code Playgroud)

  • 或这个!`System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().地点)` (9认同)
  • 如果您正在运行NUnit测试,似乎不起作用.该情况下的路径指向TEMP目录. (9认同)

Tho*_*eir 25

这就是我过去常常要完成的事情:

System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, System.AppDomain.CurrentDomain.RelativeSearchPath ?? "");
Run Code Online (Sandbox Code Playgroud)

  • 这在IIS托管和Winform中完美无缺,谢谢. (2认同)

Jam*_*ran 8

var assemblyPath = Assembly.GetExecutingAssembly().CodeBase;
Run Code Online (Sandbox Code Playgroud)


Woj*_*icz 8

Path.GetDirectoryName(Application.ExecutablePath)
Run Code Online (Sandbox Code Playgroud)

例如.值:

C:\Projects\ConsoleApplication1\bin\Debug
Run Code Online (Sandbox Code Playgroud)

  • 注意:此解决方案仅适用于Windows.Forms (6认同)