0

I need to verify that my app can connect to the default C$ share on a remote server on the network. Does anyone have a really bulletprrof way I can do this? Initially I was doing something like this:

Dim ProcessStartInfo As New System.Diagnostics.ProcessStartInfo
ProcessStartInfo.FileName = "net"
ProcessStartInfo.Arguments = "use \\" & strComputer & "\c$ /USER:" & strUsername & " " & strPassword
ProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden
System.Diagnostics.Process.Start(ProcessStartInfo)

But this can be flakey sometimes and hang, take too long to return etc. Whatever method I use needs to be able to supply credentials and if at all possible, when the connection fails I need to know why it failed.

I solved this another way but can't find the thread.

Jimmy D
  • 5,282
  • 16
  • 54
  • 70

1 Answers1

1
Dim dir As New IO.DirectoryInfo("\\10.101.1.200\c$")

dir.exists will then tell you if it was able to connect.

This will try to connect as the account the program was run from. If you need different credentials you may need to look in to Impersonation.

You could directly call the windows functions via pInvoke, but that gets more complex. An example can be found here. C#: How to logon to a share when using DirectoryInfo

Community
  • 1
  • 1
Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • Thanks. This would work except there are times when this would run in a workgroup environment and credentials could be different for each machine. I'll keep poking into your link though... – Jimmy D Jan 03 '12 at 20:00