【问题标题】:Capture the directory path it uses to run the program捕获它用于运行程序的目录路径
【发布时间】:2014-04-01 22:39:47
【问题描述】:

我有一个 C# 应用程序,假设它从 bin 目录运行

string current_directory = Directory.GetCurrentDirectory(); //get current directory, this is the bin dir
string parent_dir = Directory.GetParent(current_directory).ToString();// this is parent of bin dir

string _Config1 = parent_dir + "\\config\\x.cfg";
string _Config2 = parent_dir + "\\config\\y.cfg";
string _Log = parent_dir + "\\log\\z.log";

问题是由于某些原因,用户无法进入 bin 目录并运行应用程序(只需键入“application_name”)。他必须使用路径运行它(即d:\blah\1\blah\2\blah\3\bin\application_name

当他这样做时,他会得到

System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)

所以要么我必须捕获他用来运行程序并在我的程序中使用它的路径,要么以某种方式使我的应用程序能够使用路径运行。

【问题讨论】:

    标签: c# path system .net


    【解决方案1】:

    您可以使用AppDomain.CurrentDomain.BaseDirectory

    【讨论】:

    • 非常直接
    【解决方案2】:

    Application.StartupPath 用于WinForms

    【讨论】:

      【解决方案3】:

      你可以使用反射:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-22
        • 2011-09-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多