简体   繁体   English

使用C#使用vbs文件运行bat文件时如何使用等待退出?

[英]How do I use wait for exit when using C# to use a vbs file to run bat file?

How do I use wait for exit when using C# to use a vbs file to run bat file? 使用C#使用vbs文件运行bat文件时如何使用等待退出?

Currently, it does not wait until the vbs is done. 当前,它不等到vbs完成。 I have added the vbs code. 我已经添加了vbs代码。

 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

You are executing an other command inside that VBS script that it runs an other cmd . 您正在该VBS脚本中执行另一个命令,该命令运行另一个cmd So the VBS script it just calls the cmd command and it exits without checking if the shell run has finished. 因此,VBS脚本只调用cmd命令,然后退出而不检查外壳运行是否完成。 Try to change your VBScript like this: 尝试像这样更改您的VBScript:

oShell.Run strArgs, 0, true

Run Documentation 运行文档

.Run

Run an external Command. 运行一个外部命令。

Syntax 句法

objShell.Run (strCommand, [intWindowStyle], [bWaitOnReturn])

Last parameter says the Run to wait until finish. 最后一个参数表示Run以等待完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM