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
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 :
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. It is only understood by 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. 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. So, the easiest way is to run cmd.exe
from your program.
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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.