在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是防止窗體關閉而已。