我要检查如果一组文件路径代表一个现有的文件。
它的工作原理,当路径中包含一台机器,这不是目前网络上的网络共享,除了罚款。 在这种情况下,它需要一个相当长的时间(30或60秒)超时。
问题
有没有一种方法,以缩短非现有的网络股超时? (我敢肯定,如果它们确实存在,他们会快速回答,所以1秒的超时将被罚款)
是否有任何其他的方式来解决这个问题,而无需启动缓存,使算法更复杂? (即,我已经知道这些X网络共享不存在,跳过匹配的路径的其余部分)
更新:使用线程工作,不是特别优雅,但
public bool pathExists(string path)
{
bool exists = true;
Thread t = new Thread
(
new ThreadStart(delegate ()
{
exists = System.IO.File.Exists(path);
})
);
t.Start();
bool completed = t.Join(500); //half a sec of timeout
if (!completed) { exists = false; t.Abort(); }
return exists;
}
该解决方案避免了每尝试一个线程,首先检查该驱动器是访问,并储存在某处的需要。
专家交换解决方案 :
首先,有一个“超时”的值,你可以在IsDriveReady功能设置。 我有它设置为5秒,但将其设置为你的作品什么。
3种方法如下使用:
- 第一个是WNetGetConnection API函数获取的驱动器的UNC(\服务器\份额)
- 第二个是我们的主要方法:Button1_Click事件
- 第三是IsDriveReady功能坪服务器。
这个工作对我来说太棒了! 干得好:
'This API Function will be used to get the UNC of the drive Private Declare Function WNetGetConnection Lib "mpr.dll" Alias _ "WNetGetConnectionA" _ (ByVal lpszLocalName As String, _ ByVal lpszRemoteName As String, _ ByRef cbRemoteName As Int32) As Int32 'This is just a button click event - add code to your appropriate event Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim bIsReady As Boolean = False For Each dri As IO.DriveInfo In IO.DriveInfo.GetDrives() 'If the drive is a Network drive only, then ping it to see if it's ready. If dri.DriveType = IO.DriveType.Network Then 'Get the UNC (\\servername\share) for the ' drive letter returned by dri.Name Dim UNC As String = Space(100) WNetGetConnection(dri.Name.Substring(0, 2), UNC, 100) 'Presuming the drive is mapped \\servername\share ' Parse the servername out of the UNC Dim server As String = _ UNC.Trim().Substring(2, UNC.Trim().IndexOf("\", 2) - 2) 'Ping the server to see if it is available bIsReady = IsDriveReady(server) Else bIsReady = dri.IsReady End If 'Only process drives that are ready If bIsReady = True Then 'Process your drive... MsgBox(dri.Name & " is ready: " & bIsReady) End If Next MsgBox("All drives processed") End Sub Private Function IsDriveReady(ByVal serverName As String) As Boolean Dim bReturnStatus As Boolean = False '*** SET YOUR TIMEOUT HERE *** Dim timeout As Integer = 5 '5 seconds Dim pingSender As New System.Net.NetworkInformation.Ping() Dim options As New System.Net.NetworkInformation.PingOptions() options.DontFragment = True 'Enter a valid ip address Dim ipAddressOrHostName As String = serverName Dim data As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" Dim buffer As Byte() = System.Text.Encoding.ASCII.GetBytes(data) Dim reply As System.Net.NetworkInformation.PingReply = _ pingSender.Send(ipAddressOrHostName, timeout, buffer, options) If reply.Status = Net.NetworkInformation.IPStatus.Success Then bReturnStatus = True End If Return bReturnStatus End Function