从C#运行时命令行进程不起作用

Hou*_*man 4 c# cmd process path filepath

我正在尝试上传文件,然后使用服务器端进程进行转换.

这是在ASP.NET开发服务器localhost:8638上运行的Visual Studio Web ASP.NET Web应用程序的一部分

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();
Run Code Online (Sandbox Code Playgroud)

我可以cmd.exeData目录内手动打开,然后输入这个命令,替换文件名,它就可以了.但是,运行此代码不会产生任何结果

我错过了什么,或做错了什么?

Mik*_*cup 6

你不能只把所有东西都传递给cmd.您需要使用/ C参数,该参数将使用这些命令打开命令提示符,并在完成运行该命令时终止它.尝试将您的参数更改为

StartInfo.Arguments = "/C soffice --headless --invisible -convert-to pdf "+fn+".ppt";
Run Code Online (Sandbox Code Playgroud)

另一种解决方案是简单地运行流程(如SLaks的评论中所述).更改p.StartInfo.FileName为相应的可执行文件,编辑您的参数,您应该很高兴.这应该是首选方法,因为它可以更直接地执行您想要的操作.