使用C#使用vbs文件运行bat文件时,如何使用wait for exit?
private string SetBackUpFiles(string vbsFileName, CheckBox BackUpCheckBox)
{
Process proc = null;
string BackUpCompleted;
BackUpCompleted = "Back up function error";
try
{
proc = new Process();
proc.StartInfo.WorkingDirectory = vbsDir;
proc.StartInfo.FileName = vbsFileName;
proc.StartInfo.CreateNoWindow = false;
proc.EnableRaisingEvents = true;
proc.Start();
proc.WaitForExit();
BackUpCompleted = "Backup Completed!";
BackUpCheckBox.Checked = true;
BackUpCheckBox.Visible = true;
}
catch (Exception ex)
{
BackUpCompleted = (ex.StackTrace.ToString());
}
return BackUpCompleted;
}
--------VBS script-----
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c LayoutsBackup.bat"
oShell.Run strArgs, 0, false
答案 0 :(得分:0)
您正在该VBS脚本中执行另一个命令,它运行另一个cmd
。所以它只调用cmd
命令的VBS脚本退出而不检查shell运行是否已经完成。尝试像这样更改您的VBScript:
oShell.Run strArgs, 0, true
.Run
运行外部命令。
语法
objShell.Run (strCommand, [intWindowStyle], [bWaitOnReturn])
最后一个参数表示Run
要等到完成。