user773737
user773737

Reputation:

Command line process not working when run from C#

I'm trying to uploading a file and then using server-side processes to convert it.

This is part of a Visual Studio Web ASP.NET web application running on a ASP.NET Development server, localhost:8638

string fn = System.IO.Path.GetFileNameWithoutExtension(File1.PostedFile.FileName);
Process p = new Process();
                    p.StartInfo.WorkingDirectory = Server.MapPath("/Data");
                    p.StartInfo.FileName = "cmd.exe";
                    p.StartInfo.Arguments = "soffice --headless --invisible -convert-to pdf "+fn+".ppt";
                    p.StartInfo.UseShellExecute = false;
                    p.StartInfo.RedirectStandardOutput = true;
                    p.Start();
                    p.WaitForExit();

I can manually open cmd.exe inside of the Datadirectory, then type this command, substituting the file name, and it'll work. However, running this code does not produce any result

What am I missing, or doing wrong?

Upvotes: 4

Views: 4126

Answers (1)

Mike Precup
Mike Precup

Reputation: 4218

You can't just pass everything in to cmd. You need to use the /C parameter, which will open a command prompt with those commands and terminate it when it finishes running that command. Try changing your arguments to

StartInfo.Arguments = "/C soffice --headless --invisible -convert-to pdf "+fn+".ppt";

An alternate solution would be to simply run the process itself (as suggested in the comments by SLaks). Change p.StartInfo.FileName to the appropriate executable, edit your arguments, and you should be good to go. That should be the preferred method, as it does what you want more directly.

Upvotes: 6

Related Questions