开发者

If, Then Statement to let User Know Command Could Not execute?

Lets just say we have this as Command1

Dim Command1 = "whoami.exe >> C:\Hello.Txt"

The program will read a list of users from a text file and then perform the action on each of them. If the user does not exist, or they are part of a password protected computer, I would like to see that in my printout.

I have this but am Unsure how to write the If Then Statement (If that is the ebst route to take)

For Each strUserName as String in strLines
Shell("cmd.exe /c" & Command1)
If开发者_如何学JAVA Command1 = fail??
Then msgbox("Oops") ???


If you want to redirect the output of 'whoami.exe' to your own console, you can do the following:

Dim startInfo As New ProcessStartInfo()
startInfo.Arguments = "c:\Hello.txt"
startInfo.FileName = "c:\whoami.exe"
startInfo.RedirectStandardOutput = True
startInfo.UseShellExecute = False

Using process As Process = Process.Start(startInfo)
    Using stream As StreamReader = process.StandardOutput
        Console.Write(stream.ReadToEnd())
    End Using
End Using

You will need to import the System.Diagnostics namespace. If 'whoami.exe' returns an exit code you can use, you can also use the Process class to check it by calling:

process.WaitForExit()
Dim code As Integer = process.ExitCode
If code = 1 Then
    ' success
Else
    ' other
End If

Hope this helps.


You need to write the If Then statement in either one line or multiple lines ending with an End If

If Command1 = fail Then msgbox("Oops")

or

If Command1 = fail Then 
    msgbox("Oops")
End If

Here is the msdn documentation for the if statement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜