从其文件夹运行exe

时间:2013-08-07 07:08:49

标签: c# process executable wkhtmltopdf

这是我的方案:我有一个可执行文件,可将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);

但它不起作用。

2 个答案:

答案 0 :(得分:3)

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

  

请注意,在Windows上,您现在不能将绝对路径名与驱动器一起用于HTML文件:

     

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

     

会失败。您需要使用file:/// URL:

     

wkhtmltopdf file:/// d:/tmp/x.html x.pdf

This answer可能有助于附加

答案 1 :(得分:0)

您从哪里调用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 ..

这是我的代码

获取Current Exe可执行路径

 string sCurrentPAth =  Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

  Process.Start(@"E:\Debug\MyExe.exe", "arg1 arg2 arg3");

正确运作.....