【问题标题】:Check if server path is available as file share in C#检查服务器路径是否可用作 C# 中的文件共享
【发布时间】:2013-10-08 00:54:52
【问题描述】:

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

关于我正在尝试做什么的更多细节,我提示用户连接一个 SQL 服务器,他们给了我一个地址,例如“SQL001”;显然这个地址只有在我们的内部网络上才有效。有了这个地址,我就可以连接到服务器及其数据库。现在,我为他们提供了备份数据库的选项,并希望 OpenFileDialog 将 InitialDirectory 设置为“\\SQL001”,以便他们可以快速访问该服务器上的共享文件夹并将数据库备份到远程服务器上。

如果我将“\\SQL001”设置为 OpenFileDialog 的 InitialDirectory,一切正常,但是如果他们输入错误并输入“\\SQL002”(不存在),或者在关闭内部时尝试使用该工具网络,则 OpenFileDialog 的 ShowDialog 函数会引发错误。所以我想先检查并确保文件共享可用,否则我不会设置 InitialDirectory。

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

所以我的问题是,假设用户对文件共享具有权限,我如何检查文件共享是否可用?另外,我也不想强迫用户map "\\SQL001" as a network drive

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

【问题讨论】:

    标签: c# .net share exists unc


    【解决方案1】:

    有效的 UNC 路径必须至少包含两个组件; \SERVERNAME\SHARE;如果不满足该条件,Directory.Exists 将返回 false。

    要确定由 SERVERNAME 标识的机器是否存在,您可以使用 GetHostByName

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

    不过,这仍然不会告诉您机器是否已停机。

    【讨论】:

    • 你能告诉我如何检查本地电脑中的共享文件夹是否存在吗?我需要从袖珍电脑上测试它(使用.net compact framework)
    【解决方案2】:

    根据 Allan Elder 的回复,我想出了以下似乎可行的解决方案。我使用 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;
    }
    

    【讨论】:

    【解决方案3】:

    如果您在检查服务器可用性后打算使用 Windows 文件共享,有效的方法是打开一个套接字到 NetBIOS 端口 (139) 用于文件共享服务。这不仅会告诉您服务器是否在线,还会告诉您它是否可用于文件操作。 Complete .net source code is here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多