在服务器上,我试图打开命令提示符并调用可执行文件将文件转换为PDF.为此,我使用的是PDFCreator开源程序.
在C#中,我使用以下代码调用:
processstartinfo processstartinfo = new processstartinfo("cmd.exe"); processstartinfo.RedirectStandardInput = true; processstartinfo.RedirectStandardOutput = true; processstartinfo.UseShellExecute = false; Process process = Process.Start(processstartinfo); process.StandardInput.WriteLine(@"cd c:\program files (x86)\pdfcreator"); process.StandardInput.WriteLine(@"PDFCreator.exe /PF""c:\dwf\dwf.dwf""");
它运行时没有错误,但没有产生任何结果.此PDFCreator.exe所做的是调用另一个程序,Autodesk Design Review打开,使用PDF驱动程序打印到PDF,并保存文件.您看到的命令可以正常运行,由我独立运行.
从其他线程搜索,似乎安全性可能是我的问题.所以我去了PDFCreator和Design Review文件夹/可执行文件,并授予对NETWORK,NETWORK SERVICE,IIS_WPG,IIS_IUSRS和ASP.NET Machine帐户的完全访问权限(实现这可能是一个安全线程,但一旦我找出源代码就会禁用问题).这没有帮助.
应该注意的是,我可以使用上面的第一个命令更改目录,然后在PDFCreator和Design Review文件夹中创建“test123”文件夹.似乎我在这附近,任何想法?
解决方法
SteveCalpoly和Val Akkapeddi的评论非常有趣.
无论如何,我使用以下方法使用命令提示符运行可执行文件
无论如何,我使用以下方法使用命令提示符运行可执行文件
/// <summary> /// Executes a shell command synchronously. /// </summary> /// <param name="command">string command</param> /// <returns>string,as output of the command.</returns> public void ExecuteCommandSync(object command) { try { // create the processstartinfo using "cmd" as the program to be run,// and "/c " as the parameters. // Incidentally,/c tells cmd that we want it to execute the command that follows,// and then exit. System.Diagnostics.processstartinfo procStartInfo = new System.Diagnostics.processstartinfo("cmd","/c " + command); // The following commands are needed to redirect the standard output. // This means that it will be redirected to the Process.StandardOutput StreamReader. procStartInfo.RedirectStandardOutput = true; procStartInfo.UseShellExecute = false; // Do not create the black window. procStartInfo.CreateNowindow = true; // Now we create a process,assign its processstartinfo and start it System.Diagnostics.Process proc = new System.Diagnostics.Process(); proc.StartInfo = procStartInfo; proc.Start(); // Get the output into a string string result = proc.StandardOutput.ReadToEnd(); // display the command output. Console.WriteLine(result); } catch (Exception objException) { // Log the exception } } /// <summary> /// Execute the command Asynchronously. /// </summary> /// <param name="command">string command.</param> public void ExecuteCommandAsync(string command) { try { //Asynchronously start the Thread to process the Execute command request. Thread objThread = new Thread(new ParameterizedThreadStart(ExecuteCommandSync)); //Make the thread as background thread. objThread.IsBackground = true; //Set the Priority of the thread. objThread.Priority = ThreadPriority.Abovenormal; //Start the thread. objThread.Start(command); } catch (ThreadStartException objException) { // Log the exception } catch (ThreadAbortException objException) { // Log the exception } catch (Exception objException) { // Log the exception } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。