0

我用 C# 编写了一个小型 Windows 服务,它执行 .BAT 和 .VBS 脚本。以下代码触发 .vbs 脚本:

string path = "C:\\Path\\To\\MyScript.vbs";
Process p = new Process();
p.StartInfo = new ProcessStartInfo();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WorkingDirectory = Environment.SystemDirectory;
p.StartInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
p.StartInfo.Arguments = "/C C:\\Windows\\System32\\cscript.exe //Nologo \"" + path + "\"";
p.StartInfo.ErrorDialog = false;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.OutputDataReceived += OutputDataReceived;
p.ErrorDataReceived += OutputDataReceived;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

try
{
p.Start();
p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit();
}
catch { // }

代码编译并运行,没有任何异常。但是,可能有问题,因为 VBS 脚本直到最后才执行。在脚本中,我打开一个 telnet 会话并输入一些命令。当我在 WinForms 应用程序中运行相同的代码和相同的脚本时,一切正常。

我认为问题是由我的服务引起的,该服务作为本地系统运行。

有任何想法吗?

编辑

这是我的 VBScript:

set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run "cmd"
WScript.Sleep 1000 

WshShell.SendKeys "telnet 127.0.0.1 3000"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 2000

WshShell.SendKeys "reboot application"
WshShell.SendKeys "{ENTER}"
WScript.Sleep 2000

由于某种原因,telnet 命令永远不会执行,但进程仍然以 exitcode '0' 结束......

4

1 回答 1

0

明白了 - 当没有用户登录时,SendKeys 不起作用。代码本身工作得很好。

于 2013-09-18T13:32:13.873 回答