C# ';指定的可执行文件不是此操作系统平台的有效应用程序
我想从C#代码打印一个PDF文件,无需用户交互 我试过了,但不管用 这是我尝试的代码:C# ';指定的可执行文件不是此操作系统平台的有效应用程序,c#,printing,process,C#,Printing,Process,我想从C#代码打印一个PDF文件,无需用户交互 我试过了,但不管用 这是我尝试的代码: Process p = new Process(); p.StartInfo = new ProcessStartInfo() { CreateNoWindow = true, Verb = "print", FileName = @"G:\Visual Studio Projects\PrintWithoutGUI\PrintWithoutGUI\Courses.pdf" //
Process p = new Process();
p.StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
Verb = "print",
FileName = @"G:\Visual Studio Projects\PrintWithoutGUI\PrintWithoutGUI\Courses.pdf" //put the correct path here
};
p.Start();
我得到一个异常:
System.ComponentModel.Win32Exception:“指定的可执行文件不是此操作系统平台的有效应用程序。”`
这里有一点是肯定错误的:
文件名
不应该是您试图打印的pdf,而是您试图执行的可执行进程的路径,因此错误消息这是编写代码的正确方法
Process p = new Process();
p.StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
Verb = "print",
FileName = "PDfReader.exe", //put the path to the pdf reading software e.g. Adobe Acrobat
Arguments = "PdfFile.pdf" // put the path of the pdf file you want to print
};
p.Start();
通过添加UseShellExecute=true尝试此操作,它将打印到默认打印机,但如果要打印到特定打印,请通过指定打印机的名称参数将动词更改为动词打印到
private static void PrintByProcess()
{
using (Process p = new Process())
{
p.StartInfo = new ProcessStartInfo()
{
CreateNoWindow = true,
UseShellExecute=true,
Verb = "print",
FileName = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\doc.pdf"
};
p.Start();
}
这意味着我必须把应用程序的路径设置为.exe?这没有道理。文档说明获取或设置要启动的应用程序或文档。您使用什么工具来打印pdf,您应该指定它以便可以执行,然后输入包括pdf路径在内的参数。我使用的是Process类。现在,在添加新答案后,一切正常,但我不希望.exe应用程序被打开尝试将
UseShellExecute
设置为true
。有没有办法不打开.exe应用程序?没有,这是唯一的方法。除非您在项目中使用pdf库。其中有很多。例如,您可以使用IronPDF,但它不是免费的。除此之外,我认为这是你唯一的选择。另外,在大多数情况下,将运行的.exe将不可见,因为您已将CreateNoWindow属性设置为truei,因为.exe应用程序出现了!谢谢您可以尝试将WindowStyle=ProcessWindowStyle.Hidden,
添加到新的ProcessStartInfo()的初始化中WindowStyle
您还可以尝试将WindowStyle
设置为ProcessWindowStyle.Minimized
,如果上述操作不起作用