Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ';指定的可执行文件不是此操作系统平台的有效应用程序_C#_Printing_Process - Fatal编程技术网

C# ';指定的可执行文件不是此操作系统平台的有效应用程序

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" //

我想从C#代码打印一个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
,如果上述操作不起作用