c# - 如何将参数传递给批处理文件?

标签 c# .net process

我需要使用这样的批处理文件运行一组 *.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 filenamesqlplus和你的argumentsscotte/tiger@emp @\"c:/EMPSCRIPTS/RUNALL.sql\"

关于c# - 如何将参数传递给批处理文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5142998/

相关文章:

c# - 如何读取 Excel XML (C#)

c# - Excel 单元格中的 VSTO 对角线

c# - 将枚举视为通用吗?

.net - Parallel.ForEach 具有有序输入?

c# - 用于在字符串中查找通配符子字符串的正则表达式模式

c# - 如何从逻辑层访问 View ?

c# - Newtonsoft自定义忽略约定

java - 监控每个被调用的方法

node.js - 使用递归process.nexttick是否可以使其他进程或线程正常工作?

c++ - 共享内存和性能