使用GhostScript从Windows服务打印PDF - 如何诊断权限问题?

问题描述 投票:1回答:1

我有一个用C#编写的服务。运行以下代码。

public static bool PrintPDF(string ghostScriptPath, int numberOfCopies, string printerName, string pdfFileName)
{
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.Arguments = $@"-dPrinted -dNoCancel=true -dBATCH -dNOPAUSE -dNOSAFER -q -dNumCopies={numberOfCopies} -sDEVICE=mswinpr2 -sOutputFile=""\\spool\{printerName}"" ""{pdfFileName}""";

    startInfo.FileName = Path.Combine(ghostScriptPath, "gswin64c.exe");
    startInfo.UseShellExecute = false;
    startInfo.CreateNoWindow = true;
    startInfo.RedirectStandardError = true;
    startInfo.RedirectStandardOutput = true;

    Process process = Process.Start(startInfo);

    Console.WriteLine(process.StandardError.ReadToEnd() + process.StandardOutput.ReadToEnd());

    process.WaitForExit(30000);
    if (process.HasExited == false) process.Kill();


    return process.ExitCode == 0;
}

在Windows服务之外,它没有任何问题.在服务内部,当以本地系统运行时,GhostScript开始运行,但没有任何输出而超时。

经过一番周折,我终于将服务切换为以网络服务的方式运行,并将网络服务设置为服务exe和GhostScript exe所在文件夹的所有者(在我这样做之前,我得到了 "访问被拒绝 "的错误信息)--现在服务运行正常。

我的问题是 - 为什么网络服务可以工作,而本地系统不能?我以为本地系统有更多的权限。另外,我如何才能得到更多关于实际问题的信息?我已经找到了一个解决方法,但这只是在黑暗中的幸运一击。我不知道真正的问题是什么。

更多信息:运行Windows 10 64位,使用GhostScript v9.29。

windows-services user-permissions diagnostics
1个回答
0
投票

你需要在一个本地用户账户下运行服务,这个账户是专门为它而设的,你还需要在列表中用该用户登录一次,以使打印机列表被填充!你需要在一个本地用户账户下运行服务。

© www.soinside.com 2019 - 2024. All rights reserved.