【问题标题】:How do I get the output from my VBScript Console using C#?如何使用 C# 从我的 VBScript 控制台获取输出?
【发布时间】:2014-11-21 16:59:47
【问题描述】:

我的应用程序打开一个网站,然后运行一个 VBS 文件来进行一些数据输入。一旦完成数据输入,我想退出应用程序。

在我当前的迭代中,VBS 文件执行并且我的 C# 代码继续运行(在数据输入完成之前退出 Web 应用程序)。

Process.Start(appPath + @"external\website.url");
getAllProcesses(false);

ProcessStartInfo startInfo = new ProcessStartInfo(appPath + @"\external\UNLOCK.vbs", employeeID);

Process scriptProc = new Process();
scriptProc.StartInfo.FileName = @"cscript";
scriptProc.StartInfo.WorkingDirectory = appPath + @"external\";            
scriptProc.StartInfo.Arguments = "UNLOCK.vbs " + employeeID;
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //prevent console window from popping up
scriptProc.StartInfo.RedirectStandardError = true;
scriptProc.StartInfo.RedirectStandardInput = true;
scriptProc.StartInfo.RedirectStandardOutput = true;
scriptProc.StartInfo.ErrorDialog = false;
scriptProc.StartInfo.UseShellExecute = false;
scriptProc.Start();

scriptProc.WaitForExit(); // <-- Optional if you want program running until your script exit

Read(scriptProc.StandardOutput);
Read(scriptProc.StandardError);

while(true)
{
    String completed = Console.ReadLine();
    scriptProc.StandardInput.WriteLine(completed);
    if(completed.CompareTo("Completed") == 0)
    {
        break;
    }
}
if (scriptProc.HasExited)
{
    getAllProcesses(true);
    Application.Exit();
}
scriptProc.Close();

我只想执行

getAllProcesses(true);
Application.Exit();

仅在我从 VBS 文件中获得“已完成”的输出之后。

我的 VBS 文件有一行写着

WScript.Echo "Completed"

在最后。

【问题讨论】:

    标签: c# vbscript


    【解决方案1】:
    Process scriptProc = new Process();
    ProcessStartInfo info = new ProcessStartInfo();
    info.WorkingDirectory = appPath + @"external\";
    info.FileName = "Cscript.exe";
    info.Arguments = "UNLOCK.vbs" + employeeID;
    info.RedirectStandardError = true;
    info.RedirectStandardInput = true;
    info.RedirectStandardOutput = true;
    info.UseShellExecute = false;
    info.WindowStyle = ProcessWindowStyle.Hidden;
    scriptProc.StartInfo = info;
    scriptProc.Start();
    scriptProc.WaitForExit();
    bool exit = false;
    
    while (!scriptProc.StandardOutput.EndOfStream)
    {
        if (scriptProc.StandardOutput.ReadLine() == "Completed")
        {
            exit = true;
            break;
        }
    }
    
    if (exit == true)
    {
        getAllProcesses(true);
        Application.Exit();
    }
    

    【讨论】:

    • 建议修改您的答案以表明您建议我所做的更改,以便更容易阅读和理解答案
    猜你喜欢
    • 2011-05-22
    • 2011-10-20
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 2012-09-11
    • 1970-01-01
    相关资源
    最近更新 更多