用cmd的方式执行exe程序

在asp.net中调用process.start执行程序,需要设置运行iis进程用户的权限,比较麻烦, MS的站点上有一篇说明:
http://support.microsoft.com/default.aspx/kb/555134  (估计页面404)
换种方法,可以先执行cmd.exe,然后以参数形式调用bat文件即可,参考文章:
http://codebetter.com/blogs/brendan.tompkins/archive/2004/05/13/13484.aspx
   

此文章主要内容是:

// Get the full file path
string strFilePath = “c:\\temp\\test.bat”;


// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(“cmd.exe”);
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = “c:\\temp\\“;


// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);



// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);


// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;


// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;



// Write each line of the batch file to standard input
while(strm.Peek() != -1)
{
  sIn.WriteLine(strm.ReadLine());
}


strm.Close();


// Exit CMD.EXE
string stEchoFmt = “# {0} run successfully. Exiting”;


sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine(“EXIT”);


// Close the process
proc.Close();


// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();



// Close the io Streams;
sIn.Close();
sOut.Close();



// Write out the results.
string fmtStdOut = “<font face=courier size=0>{0}</font>”;
this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, “<br>”)));

 

.bat 的写法:

@echo off
path = %path%;.\..\Process\;
UILessRevit2018.exe
pause

 

 

 1、

@echo off:默认

path = %path%;.\..\Process\; 其中%path%是必须,.\..\Process\是相对于2018.bat的路径
UILessRevit2018.exe 是要执行的程序
pause是防止窗体关闭而已。
posted @ 2020-10-14 09:44  就一个  阅读(4187)  评论(0编辑  收藏  举报