检查服务器路径在 C# 中是否可用作文件共享

本文关键字:文件共享 是否 服务器 路径 检查 | 更新日期: 2023-09-27 18:33:26

我想快速检查文件共享在 C# 中是否可用,但对网络共享上可能存在的目录一无所知。 我发现这些帖子 1 2 3 显示了如何检查网络目录是否可用,但它们都假设我知道我想检查的目录共享是否存在。也就是说,他们想检查''''SomeServer''SomeDirectory是否可用,但我只想检查''''SomeServer是否可用。

有关我正在尝试执行的操作的更多详细信息,我提示用户输入要连接的SQL服务器,他们给了我一个地址,例如"SQL001";显然,该地址仅在我们的内部网络上有效。有了这个地址,我就可以连接到服务器及其数据库。现在,我为他们提供了备份数据库的选项,并希望 OpenFileDialog 将 InitialDirectory 设置为"''''SQL001",以便他们可以快速访问该服务器上的共享文件夹并在远程服务器上备份数据库。

如果我将"''''SQL001"设置为OpenFileDialog的InitialDirectory,一切正常,但是如果他们输入错误并输入了"''''SQL002"(不存在(,或者尝试在关闭内部网络时使用该工具,那么OpenFileDialog的ShowDialog函数会抛出错误。所以我想先检查并确保文件共享可用,如果没有,我不会设置 InitialDirectory。

不幸的是,使用 Directory.Exists("''''SQL001"( 总是返回 false。如果我做 Directory.Exists("''''SQL001''Backups"(,它可以工作,但我们有许多不同的 SQL 服务器,它们并不都有一个名为"备份"的共享,所以这不可靠。我也可以使用对我有用的 Directory.Exists("''''SQL001''c$''">(,但许多员工没有根 C:'' 的权限,但对网络共享具有权限,所以这也不是一个好的选择。

所以我的问题是,假设用户有权访问文件共享,如何检查文件共享是否可用?另外,我也不想强迫用户将"''''SQL001"映射为网络驱动器。

我现在能看到的唯一解决方案是调用OpenFileDialog的ShowDialog函数并捕获特定的异常,清除InitialDirectory,然后再次调用ShowDialog。它会起作用,但感觉有点笨拙,所以我希望有更好的解决方案。

检查服务器路径在 C# 中是否可用作文件共享

有效的 UNC 路径必须至少包含两个组件; ''服务器名称''共享;如果不满足该条件,Directory.Exists 将返回 false。

若要确定由 SERVERNAME 标识的计算机是否存在,可以使用 GetHostByName

http://msdn.microsoft.com/en-us/library/system.net.dns.gethostbyname.aspx

不过,这仍然不会告诉您机器是否关闭。

根据艾伦·埃尔德的回应,我想出了以下似乎有效的解决方案。 我使用了System.Net.Dns.GetHostEntry((而不是GetHostByName,因为GetHostByName现在已经被弃用了。

/// <summary>
/// Gets the rooted path to use to access the host.
/// Returns an empty string if the server is unavailable.
/// </summary>
/// <param name="serverName">The server to connect to.</param>
public static string GetNetworkPathFromServerName(string serverName)
{
    // Assume we can't connect to the server to start with.
    var networkPath = String.Empty;
    // If this is a rooted path, just make sure it is available.
    if (Path.IsPathRooted(serverName))
    {
        // If the path exists, use it.
        if (Directory.Exists(serverName))
            networkPath = serverName;
    }
        // Else this is a network path.
    else
    {
        // If the server name has a backslash in it, remove the backslash and everything after it.
        serverName = serverName.Trim(@"'".ToCharArray());
        if (serverName.Contains(@"'"))
            serverName = serverName.Remove(serverName.IndexOf(@"'", StringComparison.Ordinal));
        try
        {
            // If the server is available, format the network path properly to use it.
            if (Dns.GetHostEntry(serverName) != null)
            {
                // Root the path as a network path (i.e. add '' to the front of it).
                networkPath = String.Format("''''{0}", serverName);
            }
        }
        // Eat any Host Not Found exceptions for if we can't connect to the server.
        catch (System.Net.Sockets.SocketException)
        { }
    }
    return networkPath;
}

如果您打算在检查服务器可用性后使用Windows文件共享,有效的方法是打开NetBIOS端口(139(的套接字,该端口用于文件共享服务。这不仅会告诉您服务器是否在线,还会告诉您它是否可用于文件操作。完整的 .net 源代码在这里。