0

我有一个将 PDF-s 转换为图像的程序。手动运行程序时,一切正常。但是每当我尝试将程序作为计划任务运行时,ImageMagick 或 cmd(不确定)拒绝启动并跳过该步骤,导致程序停止。

我尝试在不同的用户下运行它(计划任务和以具有特权的用户身份启动进程),但似乎没有任何效果,我不理解或不知道下一步从哪里开始。

我最好的猜测是这与用户安全有关,并且不允许从 C# 中的进程/cmd 运行 imagemagick。

代码:

string args = string.Format("/k convert -density 200 \"{0}\" -quality 40 \"{1}\\{2}\"", file.FullName, imageFolder, file.Name.Replace("pdf", "png"));
using (Process proc = new Process {
    StartInfo = {
        Arguments = args,
        FileName = @"CMD.exe",
        UseShellExecute = false,
        CreateNoWindow = true,
        RedirectStandardOutput = true,
        RedirectStandardError = true
    }
}) {
    proc.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
    proc.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
    proc.Start();
    proc.WaitForExit(5000);
    proc.Kill();
}
static void p_OutputDataReceived(object sender, DataReceivedEventArgs e) {
    using (StreamWriter sw = new StreamWriter(Program.BaseFolder + "\\log.txt")) {
        sw.WriteLine(e.Data);
    }
}
static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e) {
    using (StreamWriter sw = new StreamWriter(Program.BaseFolder + "\\log.txt")) {
        sw.WriteLine(e.Data);
    }
}
4

2 回答 2

0

您正在运行 cmd.exe 作为文件名。您似乎没有指定要在任何地方运行的实际可执行文件。我建议尝试更换:

FileName = @"CMD.exe"

FileName = @"<the executable you want to run>"
于 2013-02-18T13:09:19.000 回答
0

似乎调度任务和用户不共享相同的环境路径。

所以我重新安装了 ImageMagick、GhostScript 并在程序中使用了绝对路径。这解决了我的问题。但我并不完全知道我所做的所有这三件事,使用绝对路径可能单独解决它。

还删除了计时器proc.WaitForExit()proc.Kill()因为该过程不再启动CMD.exe

string args = string.Format("-density 200 \"{0}\" -quality 40 \"{1}\\{2}\"", file.FullName, imageFolder, file.Name.Replace("pdf", "png"));
using (Process proc = new Process {
    StartInfo = {
        Arguments = args,
        FileName = @"c:\imagemagick\convert",
        UseShellExecute = false,
        CreateNoWindow = true,
        RedirectStandardOutput = true,
        RedirectStandardError = true
    }
}) {
    proc.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
    proc.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
    proc.Start();
    proc.WaitForExit();
}
于 2013-02-19T08:17:36.947 回答