如何使用 C# 从我的 VBScript 控制台获取输出? [英] How do I get the output from my VBScript Console using C#?

查看:35
本文介绍了如何使用 C# 从我的 VBScript 控制台获取输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My application opens a website then runs a VBS file to do some data input. Once it's completed the data input, I want to quit the application.

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

In my current iteration, the VBS file executes and my C# code keeps going (quitting the web app before the data entry is completed).

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 文件中获得显示已完成"的输出后.

ONLY After I get output from my VBS file that says "Completed".

我的 VBS 文件有一行写着

My VBS file has a line that says

WScript.Echo "Completed"

最后.

推荐答案

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();
}

这篇关于如何使用 C# 从我的 VBScript 控制台获取输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆