2012-01-03 50 views
0

我需要验证我的应用程序可以连接到网络上远程服务器上的默认C $共享。有没有人有一个真正的bulletprrof方式我可以做到这一点?起初,我在做这样的事情:检查UNC连接

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) 

但这可能是有时古怪,挂,需要很长时间才能恢复等。无论采用哪种方式我使用需要能够提供凭据,如果可能的话,当连接失败我需要知道它为什么失败。

回答

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

dir.exists会告诉你它是否能够连接。

这将尝试连接作为程序运行的帐户。如果您需要不同的凭据,您可能需要查看模拟。

你可以通过pInvoke直接调用windows函数,但这会变得更加复杂。一个例子可以在这里找到。 C#: How to logon to a share when using DirectoryInfo

+0

谢谢。这可以工作,除非有时候这会在工作组环境中运行,并且每台机器的凭据可能不同。尽管如此,我会继续探索你的链接...... – 2012-01-03 20:00:09