简体   繁体   English

VB.net运行过程不起作用-命令行是?

[英]VB.net run process not working - command line is?

At a command line I can run the folowwing command: 在命令行中,我可以运行以下命令:

openfiles /query /s \\pha-sr-myserver /v > s:\textfile.txt

This runs fine and produces a list of openfiles on server pha-sr-myserver and outputs it to textfile.txt 这样可以很好地运行并在服务器pha-sr-myserver上生成一个打开文件列表,并将其输出到textfile.txt

Now I am tryng to reproduce this in code, but it's not working: 现在,我尝试在代码中重现此代码,但它不起作用:

Dim MyProcess As Process
Dim cText As String
Dim Shellcommand As String = "c:\windows\system32\openfiles.exe"
Dim ShellArgs As String = "/query /s \\pha-sr-myserver /v > S:\textfile.txt"
MyProcess = Process.Start(Shellcommand, ShellArgs)
MyProcess.WaitForExit()

this goes past the stage, but the file is never produced and for the life of me I can not see whats wrong. 这已经超过了阶段,但是该文件从未生成过,对于我的一生,我看不到出了什么问题。 I've tryed encapsulating the arguments in double quotes etc. I've shelled like this many times but can't seam to get this working. 我已经尝试将参数用双引号引起来,等等。我已经多次进行了这样的操作,但是无法进行接缝以使其正常工作。 Any ideas would be much apreciated. 任何想法都会被感动。 Thanks Graham 谢谢格雷厄姆

You can try a simple Shell command : 您可以尝试一个简单的Shell命令

Dim sComando As String
Dim Shellcommand As String = "c:\windows\system32\openfiles.exe"
sComando = """" & Shellcommand & """" & " /query /s \\pha-sr-myserver /v > S:\textfile.txt"
Shell(sComando , AppWinStyle.Normal, True)

or maybe this one: 也许这个:

Dim MyProcess As Process
Dim cText As String
Dim Shellcommand As String = "c:\windows\system32\openfiles.exe"
Dim oShellArgs As ProcessStartInfo


oShellArgs = New ProcessStartInfo(Shellcommand , " /query /s \\pha-sr-myserver /v ")  ' remove this > "  "S:\textfile.txt"" ")
oShellArgs.RedirectStandardOutput = true
oShellArgs.RedirectStandardError = true
MyProcess = Diagnostics.Process.Start(oShellArgs)

MyProcess.StandardOutput.ReadToEnd() ' read this list of string

MyProcess.WaitForExit()

The > S:\\textfile.txt is not understood by openfiles.exe. openfiles.exe无法理解> S:\\textfile.txt It is only understood by cmd.exe. 只有cmd.exe可以理解。 You need to either use Process.Start to start cmd.exe or start your app directly and then redirect its standard output to capture it. 您需要使用Process.Start来启动cmd.exe或直接启动您的应用程序,然后重定向其标准输出以捕获它。 Try something like this: 尝试这样的事情:

Dim shellArgs As String = "/query /s \\pha-sr-myserver /v"
Dim output As String = runCommandLineApp("c:\windows\system32\openfiles.exe", Nothing, shellArgs)
'Do something with output here, such as write it to a file.

Private Function runCommandLineApp(appFileName As String, workingDirectory As String, Optional args As String = Nothing) As String
    Dim psi As New ProcessStartInfo(appFileName, args)
    psi.RedirectStandardOutput = True
    psi.UseShellExecute = False
    psi.CreateNoWindow = True
    psi.WindowStyle = ProcessWindowStyle.Hidden

    If workingFolder IsNot Nothing Then
        psi.WorkingDirectory = workingFolder
    End If

    Dim p As New Process()
    p.StartInfo = psi
    p.Start()

    Dim output As String = p.StandardOutput.ReadToEnd()

    p.WaitForExit()

    Return output

End Function

Only cmd.exe can interpret > as redirection. 只有cmd.exe可以将>解释为重定向。 So, the easiest way is to run cmd.exe from your program. 因此,最简单的方法是从程序中运行cmd.exe

Dim MyProcess As Process
Dim cText As String
Dim Shellcommand As String = "cmd"
Dim ShellArgs As String = "/c openfiles /query /s \\pha-sr-myserver /v > S:\textfile.txt"
MyProcess = Process.Start(Shellcommand, ShellArgs)
MyProcess.WaitForExit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM