我需要使用这样的批处理文件运行一组 *.sql 文件。
private void btn_Execute_Click(object sender, EventArgs e)
{
try {
//Creating A batch file to execute the scripts using SQLPLUS....
FileInfo fi5 = new FileInfo("c:/EMPSCRIPTS/execute.bat");
StreamWriter sw2 = fi5.CreateText();
sw2.WriteLine("@Echo Off \r \nsqlplus scotte/tiger@emp @\"c:/EMPSCRIPTS/RUNALL.sql\" \r \nEXIT ");
sw2.Close();
System.Diagnostics.Process proc; // Declare New Process
proc = System.Diagnostics.Process.Start(ConfigurationManager.AppSettings["BatFilePath"].ToString()); // run test.bat from command line.
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.Arguments = "/c" + ConfigurationManager.AppSettings["BatFilePath"].ToString();
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StandardOutput.ReadToEnd();
proc.WaitForExit();
proc.Close();
}
catch (Exception ex) {
MessageBox.Show("Insertion Completed");
}
}
但我想停止一些不必要的文件执行。我找到了传递参数的选项。我想静态地向文件提供参数。根据用户输入的参数必须执行。有人可以帮助我吗? 提前致谢。
最佳答案
您在进程启动后更改 StartInfo 的值,这对进程没有影响。 See the "Remarks" section here for more information.
创建 ProcessStartInfo 的新实例,根据需要进行设置,然后将其传递到 the overload of Start它需要这种类型的一个实例。
此外,一旦更改了代码,您可能可以跳过将命令行写入批处理文件的过程。您的executable filename是 sqlplus
和你的arguments是scotte/tiger@emp @\"c:/EMPSCRIPTS/RUNALL.sql\"
关于c# - 如何将参数传递给批处理文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5142998/