我正在通过C#应用程序启动VBScript。在开始之前,我以编程方式更改VBScript的一些内容:使用计算机主机名交换占位符关键字。 VBScript执行但使用占位符关键字而不是计算机主机名。但是,当在Process.Start()前面停止代码时,我可以看到VBScript已成功修改并保存,包含计算机主机名。 我还尝试在执行前添加Thread.Sleep,但它没有帮助。
之前有效,我更新了程序中一些不相关的内容,现在大部分时间都无法正常工作。当我使用调试器时,有时VBScript加载了正确的内容。但最终的计划几乎一直都失败了。
任何建议这可能是什么?
以下是C#流程执行的代码:
if (boEditStartupFileFirst)
{
string[] stFileContent = File.ReadAllLines(stFullPath);
for (int i = 0; i < stFileContent.Length; i++)
{
try
{
stFileContent[i] = stFileContent[i].Replace("NAME", stHostname);
}
catch { }
}
File.Delete(stFullPath);
StreamWriter swWriter = new StreamWriter(stFullPath, true);
for (int i = 0; i < stFileContent.Length; i++)
{
swWriter.WriteLine(stFileContent[i]);
}
swWriter.Close();
}
Process p = new Process();
p.StartInfo.FileName = stFullPath;
p.StartInfo.Arguments = stArgs;
p.Start();
以下是VBScript的内容:
If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, WScript.ScriptFullName & " /elevate", "", "runas", 1
WScript.Quit
End If
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "regedit"
WScript.Sleep 500
WshShell.SendKeys "%DM"
WScript.Sleep 200
WshShell.SendKeys "NAME"
WScript.Sleep 200
WshShell.SendKeys "{ENTER}"
VBScript的作用:打开RegEdit并连接到远程计算机。