c# - Vbs 脚本可以在 CMD 中正常工作,但在代码中不能正常工作
问题描述
当我通过代码执行脚本时,它会进入无限循环。但是在 CMD 中工作的相同脚本
尝试使用 CMD 并且它可以工作但不能在代码中工作。以任意 VB Script 为例进行验证。
private string excecuteScript(string retValue)
{
try
{
string filetoexecute = Path.Combine(Application.StartupPath, Path.GetExtension(ScriptName).EndsWith(".vbs",StringComparison.InvariantCultureIgnoreCase) ? "AA_MyVB.vbs" : "AA_MyJS.js");
filetoexecute = string.Format("\"" + filetoexecute + "\"");
string arguments = " " + filetoexecute + " " + ScriptInputPara + " \"" + ScriptName + "\" ";
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = Path.Combine(Environment.SystemDirectory, "WScript.exe"),
Arguments = arguments,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = true
};
var myProcess = CommonMethods.InvokeProcess(startInfo);
StreamReader errorStream = myProcess.StandardError;
StreamReader outputStream = myProcess.StandardOutput;
string theOutput = string.Empty;
bool stopWhile = false;
while (!stopWhile)
{
stopWhile = myProcess.HasExited;
if (CmdTask.IsTaskTimedout)
{
stopWhile = true;
myProcess.Kill();
}
}
theOutput += outputStream.ReadLine();
TheError += errorStream.ReadToEnd();
if (myProcess.ExitCode != 0)
{
TheError = "Error occurred while executing script file";
}
if (IsScriptTask && !string.IsNullOrEmpty(theOutput) && !string.IsNullOrEmpty(ScriptOutputPara))
CmdTask.SetVariableValue(ScriptOutputPara, theOutput);
}
catch (Exception ex)
{
retValue = (ex.Message);
}
return retValue;
}
实际:myProcess.HasExited
退货false
预期:myProcess.HasExited
回报true
如果我theOutput += outputStream.ReadLine();
在while循环之前执行然后myProcess.HasExited
返回true ...
解决方案
推荐阅读
- java - 如何获得拖动的屏幕长度?
- android - 简单的发布请求在 android 9 中不起作用
- kubernetes - 在 Kubernetes 上转发部署时出现 HTTP/2 302 错误
- linux - 如何从我的 Ansible playbook 中检查环境变量?
- azure - AAS - Invoke-ProcessPartition:身份验证失败
- groovy - 如果在 Groovy 中存在相同 ID 的值,则将 NULL 替换为值
- javascript - 包装当前位于字符串中的 HTML 标记?
- spring - 限制spring显示的页数
- dictionary - 我需要在 dict 中获取列表中的所有键和值,但只返回最后一个键和值
- java - 在 Java 中使用 Bloom Tree 进行多集成员资格测试