我正在尝试从我的VB程序中运行cmd.exe中的shadow命令但是由于某种原因它不会运行命令我尝试了一些不同的东西,但都没有用。我能够将命令保存到批处理文件,然后成功执行它,但我更愿意直接将参数/命令传递给命令提示符并执行此方法。基本上,我有一个用户运行我创建的另一个程序来提取sessionid和服务器名称(然后我只是取消服务器号码)。他们得到一个4位数的密码,其中前两个是sessionid,最后两个是服务器号码(我们所有的服务器都被命名为smdts-(一个号码),所以我只关心他们所在的服务器号码)然后我拿四位数代码并将其插入我的影子管理程序。以下是不的当前代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SessId As String
Dim PassCode As String
Dim ServNum As String
Dim Prc As Process
PassCode = TextBox2.Text
SessId = PassCode.Substring(0, 2)
ServNum = PassCode.Substring(PassCode.Length - 2)
Dim fileargs As String = " shadow" + " " & SessId + " " + "/server:smdts-" + ServNum
Dim Pinfo = New System.Diagnostics.ProcessStartInfo
Pinfo.FileName = "cmd.exe"
Pinfo.Arguments = fileargs
Pinfo.ErrorDialog = False
Pinfo.UseShellExecute = False
Pinfo.CreateNoWindow = False
Pinfo.WindowStyle = ProcessWindowStyle.Normal
Pinfo.RedirectStandardOutput = False
Pinfo.RedirectStandardInput = True
Pinfo.RedirectStandardError = False
Prc = New Process
Prc.StartInfo = Pinfo
Prc.Start()
End Sub
什么工作(我不想使用的批处理文件):
Public Class Form1
Public pathvar As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
Dim SessId As String
Dim PassCode As String
Dim ServNum As String
PassCode = TextBox2.Text
SessId = PassCode.Substring(0, 2)
ServNum = PassCode.Substring(PassCode.Length - 2)
Dim fileargs As String = " shadow" + " " & SessId + " " + "/server:smdts-" + ServNum
Dim Streamwriter As StreamWriter
Streamwriter = File.CreateText(pathvar + "\ShadowBatch.bat")
Streamwriter.WriteLine(fileargs)
Streamwriter.Close()
Shell(pathvar + "\ShadowBatch.bat")
End Sub
任何关于为什么第一个例子不起作用的帮助将非常感谢!谢谢!
Tom K
答案 0 :(得分:1)
您将shadow ...
作为CMD的参数传递
CMD不支持。
相反,您需要传递/c shadow ...
,这将告诉CMD执行该命令并退出。
或者,您可以直接运行shadow
,而无需通过CMD。