从其文件夹中运行exe

本文关键字:运行 exe 文件夹 | 更新日期: 2023-09-27 18:26:44

这是我的场景:我有一个可执行文件,可以将html文件转换为pdf。只有从其文件夹启动此exe时,它才能工作。示例:exe位于C:''HtmlToPdf中,因此,在提示中我将执行以下操作:

C:'> cd HtmlToPdf
C:'HtmlToPdf> htmltopdf.exe htmlFile pdfFile

那么,在c#中有一种方法可以做到这一点吗?因为我试过这个:

FileInfo htmlInfo = new FileInfo(executablePath + @"'" + filename);
var procInfo = new ProcessStartInfo("wkhtmltopdf.exe",htmlInfo.FullName + " " + htmlInfo.FullName.Replace(".html",".pdf"));
procInfo.WorkingDirectory=executablePath;
procInfo.UseShellExecute = false;
Process.Start(procInfo);

但它不起作用。

从其文件夹中运行exe

wkhtmltopdf的文档/wiki表示,如果使用文件的完整路径,它将很难找到文件。您需要将file:///附加到文件名的开头

请注意,在Windows上,您目前不能对HTML文件的驱动器使用绝对路径名:

wkhtmltopdf d:''temp''x.html x.pdf

将失败。您需要使用文件:///URLs:

wkhtmltopdffile:///d:/tmp/x.htmlx.pdf

这个答案可能有助于附加

从您调用EXE的位置。无论是Windows窗体应用程序还是Webforms。。。。

If its windows forms it will work
else
if its Webforms like asp.net you have to change the properties of IIS Server to start the exe
Because due to some security reasons microsoft will not allow you to start the process class from IIS server... but the same code will work from Visualstudio ..

这是我的代码

获取当前Exe可执行路径

 string sCurrentPAth =  Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  Process.Start(@"E:'Debug'MyExe.exe", "arg1 arg2 arg3");

它工作正常。。。。。