获取项目路径

时间:2017-01-27 09:02:19

标签: c# asp.net-mvc

我有一个项目很少的解决方案。

解决方案1 ​​

Pro1(MVC项目), Pro2(类库),和 Pro3(班级图书馆)

所以我使用Pro 3作为报告部分,并使用hkHtmlToPDF将htmls导出为pdf。有一个名为hkHtmlToPdf.exe的exe用于执行我的任务。 但我的问题很少。执行代码需要exe路径。我想获得Pro3物理路径并使用下面的代码来获取路径。

var myPath = System.AppDomain.CurrentDomain.BaseDirectory;

以上代码返回Web项目路径。其实我需要Pro3路径。如何获得它?

2 个答案:

答案 0 :(得分:1)

您知道您的exe文件的名称。

string pattern = "hkHtmlToPdf.exe";
string dirPath = AppDomain.CurrentDomain.BaseDirectory;
Directory.GetFiles(dirPath , pattern, SearchOption.AllDirectories);

编辑:
一切正常!创建2个控制台应用程序如下:

static void Main(string[] args)
{
    Console.WriteLine("This exe is runned from another exe.");
    Console.ReadKey();
}
static void Main(string[] args)
{
    string pattern = "ConsoleApplication1.exe";
    string dirPath = AppDomain.CurrentDomain.BaseDirectory;
    var files = Directory.GetFiles(dirPath, pattern, SearchOption.AllDirectories);
    if (files.Length > 0) Process.Start(files[0]);
    else Debug.WriteLine("File not found");
    Console.ReadKey();
}

将它们放在一个文件夹中,运行App2。它将找到App1并运行。即使App1将在子文件夹中!

答案 1 :(得分:-1)

您可以在web.config中将路径添加为配置参数,而不是搜索。

相关问题