如何将参数传递给批处理文件?
我需要使用这样的批处理文件运行一组 *.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");
}
}
但我想停止一些不必要的文件执行。我找到了传递参数的选项。我想静态地向文件提供参数。根据用户输入的参数必须执行。有人可以帮助我吗? 提前致谢。
I have requirement to run a set of *.sql files using batch file like this.
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");
}
}
But I want to stop some files being executed unnecessarily. I found the option for passing the parameters. I want to give the parameters to the files staticly. Based on the users input that parameter has to execute. Could any one help me?
Thanks in Advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在进程启动后更改 StartInfo 的值,这对进程没有影响。 有关详细信息,请参阅此处的“备注”部分。
创建 ProcessStartInfo 的新实例,并设置它添加您需要的内容,然后将其传递到 Start 的重载获取此类型的一个实例。
此外,一旦更改了代码,您可能可以跳过将命令行写入批处理文件的过程。您的可执行文件名是
sqlplus< /code> 和您的参数是
scotte/tiger@emp @\"c:/EMPSCRIPTS/RUNALL.sql\"
You're changing the values of StartInfo after the process has been started, which has no effect on the process. See the "Remarks" section here for more information.
Create a new instance of ProcessStartInfo, set it up with what you need, then pass it into the overload of Start that takes an instance of this type.
In addition, once you change your code around, you can probably skip writing the command line to the batch file. Your executable filename is
sqlplus
and your arguments arescotte/tiger@emp @\"c:/EMPSCRIPTS/RUNALL.sql\"
您误用了
Process.Start
。您需要创建一个新的 ProcessStartInfo 对象,设置其所有属性,然后将其传递给 Process.Start。
正如您的代码所做的那样,修改已运行进程的
StartInfo
没有任何效果。You're misusing
Process.Start
.You need to create a new
ProcessStartInfo
object, set all of its properties, then pass it toProcess.Start
.Modifying the
StartInfo
of an already-running process, as your code is doing, has no effect.