c#无法使用asp.net运行带有自定义参数的进程(cmd)
我有一个小型asp.net web表单应用程序。我正在尝试使用给定的命令运行cmd.exe。我正在将项目发布到服务器,因此无法在服务器上调试它 首先,我尝试了dir命令,该命令正在正常工作c#无法使用asp.net运行带有自定义参数的进程(cmd),c#,asp.net,windows,C#,Asp.net,Windows,我有一个小型asp.net web表单应用程序。我正在尝试使用给定的命令运行cmd.exe。我正在将项目发布到服务器,因此无法在服务器上调试它 首先,我尝试了dir命令,该命令正在正常工作 System.Diagnostics.Process si = new System.Diagnostics.Process(); si.StartInfo.WorkingDirectory = "C:\\"; si.StartInfo.UseShellExecu
System.Diagnostics.Process si = new System.Diagnostics.Process();
si.StartInfo.WorkingDirectory = "C:\\";
si.StartInfo.UseShellExecute = false;
si.StartInfo.FileName = "cmd.exe";
si.StartInfo.UserName = "Administrator";
si.StartInfo.Password = new SecureString();
si.StartInfo.Arguments = @"/c dir";
si.StartInfo.CreateNoWindow = true;
si.StartInfo.RedirectStandardInput = true;
si.StartInfo.RedirectStandardOutput = true;
si.StartInfo.RedirectStandardError = true;
foreach (char c in "pass*")
si.StartInfo.Password.AppendChar(c);
si.Start();
string error = si.StandardError.ReadToEnd();
string output = si.StandardOutput.ReadToEnd();
si.WaitForExit();
si.Close();
if (!String.IsNullOrEmpty(error.Trim()))
{
throw new Exception("Command cannot be executed");
}
//Pre-created .bat file
if (File.Exists(@"C:\test\s.bat"))
{
using (StreamWriter sw = File.AppendText(@"C:\test\s.bat"))
{
sw.WriteLine(output);
}
}
**当我检查.bat文件时,它被写入c:/下的所有目录,因此它工作正常
其次,我尝试发送自定义命令以使用cmd.exe执行
传递的命令(参数)是:
C:\dev6i\BIN\RWRUN60.EXE userid=user/pass report="h:\avicenna\uyg6i\Mikr2392" BACKGROUND="NO" BATCH="YES" DESFORMAT="pdf" DESNAME="C:\test\reports\report_L_23123_121213.pdf" DESTYPE="FILE" PARAMFORM="NO" PATID="131233" PRONO="6472886" GROUB="3"
它正在执行一个带有一些参数的报告程序(RWRUN60.exe),该程序触发另一个程序(在h:/network文件夹中)在本地计算机的“C:\test\reports\”文件夹中创建报告pdf。当我将此文本复制并粘贴到命令行(cmd.exe)时,它可以正常工作,并且创建了pdf。
以下是此操作的代码:
System.Diagnostics.Process si = new System.Diagnostics.Process();
si.StartInfo.WorkingDirectory = @"C:\test\";
si.StartInfo.UseShellExecute = false;
si.StartInfo.FileName = "cmd.exe";
si.StartInfo.UserName = "Administrator";
si.StartInfo.Password = new SecureString();
si.StartInfo.Arguments = result.command; //result.command is the argument text above
// si.StartInfo.Arguments = @"/c "+result.command; //also tried this , unsure /c is necessary
si.StartInfo.CreateNoWindow = true;
si.StartInfo.RedirectStandardInput = true;
si.StartInfo.RedirectStandardOutput = true;
si.StartInfo.RedirectStandardError = true;
foreach (char c in "pass*")
si.StartInfo.Password.AppendChar(c);
si.Start();
string error = si.StandardError.ReadToEnd();
string output = si.StandardOutput.ReadToEnd();
si.WaitForExit();
si.Close();
if (!String.IsNullOrEmpty(error.Trim()))
{
throw new Exception("Error occured");
}
if (File.Exists(@"C:\test\s.bat"))
{
using (StreamWriter sw = File.AppendText(@"C:\test\s.bat"))
{
sw.WriteLine(output);
}
}
**使用这些代码,它只会在运行时出错。但是由于某种原因,pdf没有创建,因此该命令不起作用。测试文件夹具有完全控制的IIS_USR权限。我几乎可以肯定这与参数有关,但不知道该怎么办。
我应该检查什么 当你在你的机器上完全运行它时,它能工作吗。。?此外,应用程序可能无权在每个人的本地计算机上创建pdf。。也许您应该创建一个网络共享,并将文件放在一个位置,而不是多个分散的位置。。另外,如果文件路径/目录不存在怎么办如果(file.Exists(@“C:\test\s.bat”)如果目标已经是可执行文件,为什么还要运行
cmd.exe
?只需执行RWRUN60.EXE
并将参数传递给它。还请注意,如果工作目录设置为C:\test
,而不是它所在的目录(C:\dev6i\BIN
),则执行RWRUN60.EXE
)可能会失败,因为它依赖于该目录中的一些DLL。感谢您的回复,我还将命令文本保存到.bat文件中,并运行它:Process.Start(@“C:\test\s.bat”)。。。。它只是启动和关闭进程,但文件并没有在所需的文件夹中创建。我不明白,当我手动单击.bat文件时,它只是在工作,并且创建了文件,但当像这样调用时它不工作:Process.Start(@“C:\test\s.bat”)