G
Guest
The code below uses System.Diagnostics.Process to call Net.exe to delete a
mapped network drive. It works well except when the mapped drive is in use in
which case Net.exe prompts the "user" to confirm the drive should be deleted.
I'm sure I should be able to reply to this question by redirected the
Process's StandardInput but despite hours of trying I can't see how. Is it
possible?
Private Sub UnMapNetworkDrive(ByVal DriveLetter As String)
Dim _Process As New System.Diagnostics.Process()
With _Process
Try
With .StartInfo
.FileName = String.Format("{0}\Net.exe",
System.Environment.GetFolderPath(Environment.SpecialFolder.System))
.Arguments = String.Format("use {0}: /delete",
DriveLetter)
.CreateNoWindow = True
.UseShellExecute = False
.RedirectStandardError = True
.RedirectStandardOutput = True
End With
.Start()
.WaitForExit()
Dim _StandardError As String = .StandardError.ReadToEnd
If Not _StandardError = String.Empty Then Throw New
ApplicationException(_StandardError)
Dim _StandardOutput As String = .StandardOutput.ReadToEnd
If Not _StandardOutput.StartsWith(String.Format("{0}: was
deleted successfully.", DriveLetter)) Then Throw New
ApplicationException("The network drive was not unmapped.")
Finally
.Close()
End Try
End With
End Sub
mapped network drive. It works well except when the mapped drive is in use in
which case Net.exe prompts the "user" to confirm the drive should be deleted.
I'm sure I should be able to reply to this question by redirected the
Process's StandardInput but despite hours of trying I can't see how. Is it
possible?
Private Sub UnMapNetworkDrive(ByVal DriveLetter As String)
Dim _Process As New System.Diagnostics.Process()
With _Process
Try
With .StartInfo
.FileName = String.Format("{0}\Net.exe",
System.Environment.GetFolderPath(Environment.SpecialFolder.System))
.Arguments = String.Format("use {0}: /delete",
DriveLetter)
.CreateNoWindow = True
.UseShellExecute = False
.RedirectStandardError = True
.RedirectStandardOutput = True
End With
.Start()
.WaitForExit()
Dim _StandardError As String = .StandardError.ReadToEnd
If Not _StandardError = String.Empty Then Throw New
ApplicationException(_StandardError)
Dim _StandardOutput As String = .StandardOutput.ReadToEnd
If Not _StandardOutput.StartsWith(String.Format("{0}: was
deleted successfully.", DriveLetter)) Then Throw New
ApplicationException("The network drive was not unmapped.")
Finally
.Close()
End Try
End With
End Sub