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

The*_*one 7 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" //put the correct path here
};
p.Start();
Run Code Online (Sandbox Code Playgroud)

我得到这个例外:

System.ComponentModel.Win32Exception:“指定的可执行文件不是此操作系统平台的有效应用程序。”

big*_*heo 9

通过添加 UseShellExecute=true尝试此操作,它将打印到默认打印机,但如果您想打印到特定打印,请通过指定打印机参数属性的名称将动词print 更改为动词 printTo 。

 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();
        }
Run Code Online (Sandbox Code Playgroud)

  • 是的,.NET Core 中的更改要求我们现在使用“UseShellExecute”。请参阅 https://github.com/dotnet/runtime/issues/17938 (2认同)

小智 2

这是编写代码的正确方法

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();
Run Code Online (Sandbox Code Playgroud)