我有一个将 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);
}
}