An attempt to invoke the command line is performed in C # / ASP.NET / IIS 7.5 / Win7

On the server, I try to open a command prompt and call an executable file that converts the file to PDF. For this, I use the open source program PDFCreator.

In C #, I'm calling with this code:

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"""); 

It works without errors, but does not give a result. What this PDFCreator.exe file does is call another Autodesk Design Review program that opens, uses the PDF driver to print to PDF, and saves the file. The command you see works great and works autonomously.

From clearing other threads, it seems security may be my problem. So I went to the folders / executables of the PDFCreator and Design Review and granted full access to NETWORK, NETWORK SERVICE, IIS_WPG, IIS_IUSRS and the ASP.NET Machine account (you understand that this is probably a security thread, but it turns off when I find out the source problem). It did not help.

It should be noted that I can change the directory using the first command above, and then create the "test123" folder in the PDFCreator and Design Review folders. Seems like I'm getting closer here, any ideas?

+4
source share
4 answers

A problem has been detected on the server. The application that I called should open another application on the desktop, and then print to PDF. IIS cannot open programs on the desktop if they are not installed on the services --> service name --> log .

Unfortunately, the application I'm calling is not in the services panel, so I am stuck again now, but at least I know this is not a C # problem.

0
source

The comments by SteveCalPoly and Val Akkapeddi are very interesting. Anyway, I use the following methods to run the command line executable

  /// <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 } } 
+2
source

There is a WaitForExit () method in the System.Diagnostics.Process class, which will wait until its running process terminates before continuing, and then returns a return code.

Try creating a batch file with your commands, and then run the batch file through the Process class. What happens if you use Process.WaitForExit (); after calling Process.Start (processInfo) ;? Is there any return code from process.WaitForExit () at all?

0
source

Perhaps the errors go to the StandardError stream and therefore you do not see them?

Also, why not invoke PDFCreator.exe directly and not through cmd.exe?

Try something like this

 ProcessStartInfo processStartInfo = new ProcessStartInfo(@"c:\program files (x86)\pdfcreator"); processStartInfo.Arguments = @"/PF""c:\dwf\dwf.dwf""" processStartInfo.RedirectStandardInput = true; processStartInfo.RedirectStandardOutput = true; processStartInfo.RedirectStandardError = true; processStartInfo.UseShellExecute = false; Process process = Process.Start(processStartInfo); // Read the output stream first and then wait. string output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); string errors = p.StandardError.ReadToEnd(); 
0
source

Source: https://habr.com/ru/post/1392817/


All Articles