我如何获得Bin Path?

问题描述 投票:63回答:5

我需要执行程序集的bin路径。你怎么得到的?我在Bin / Debug中有一个文件夹插件,我需要获取该位置

c# .net
5个回答
96
投票

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

var path = System.IO.Path.GetDirectoryName( 
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

MSDN对how to determine the Executing Application's Path有完整的参考。

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

path = path.Substring(6);

60
投票

你可以做到这一点

    Assembly asm = Assembly.GetExecutingAssembly();
    string path = System.IO.Path.GetDirectoryName(asm.Location);

21
投票

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

System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, System.AppDomain.CurrentDomain.RelativeSearchPath ?? "");

8
投票
var assemblyPath = Assembly.GetExecutingAssembly().CodeBase;

8
投票
Path.GetDirectoryName(Application.ExecutablePath)

例如。值:

C:\Projects\ConsoleApplication1\bin\Debug
© www.soinside.com 2019 - 2024. All rights reserved.