我想通过在cmd中执行"查询会话"来获取结果.exe并使用 VB.NET 将它们存储在字符串变量中。我已经搜索了两天了,我无法运行该命令,一旦我运行命令,我将尝试保存结果。
似乎VisualStudio执行cmd.exe但不是C:\Windows\System32中的cmd。
Dim process As System.Diagnostics.Process = New System.Diagnostics.Process
process.StartInfo.FileName = "C:Windowssystem32cmd.exe"
process.StartInfo.Arguments = "/K query session"
process.StartInfo.UseShellExecute = True
process.StartInfo.CreateNoWindow = True
process.StartInfo.RedirectStandardOutput = False
process.Start()
process.WaitForExit()
它返回错误:"查询"未被识别为内部或外部命令,可操作的程序或批处理文件。
这似乎是一个权限问题。您需要将管理员用户的用户名和密码传递给您的命令
.....
process.StartInfo.UserName = "yourAdminUser"
process.StartInfo.Password = GetPassword()
.....
Public Function GetPassword() as String
Dim ss = new SecureString()
ss.AppendChar("p")
ss.AppendChar("a")
ss.AppendChar("s")
ss.AppendChar("s")
return ss
End Function
注意:SecureString 类需要对 System.Security.dll
的引用和命名空间的导入System.Security