如何使用WNetCancelConnection2正确关闭连接?

8
我想在需要时访问共享并取消访问。 我使用以下代码:
class Program
{
    const string Share = "\\\\srv\\share";

    static void ListFiles()
    {
        foreach (var dir in Directory.EnumerateDirectories(Share))
            Console.WriteLine(dir);
    }

    static void Main(string[] args)
    {
        using (var connection = new NetworkConnection(Share, new System.Net.NetworkCredential("user", "password")))
            ListFiles();
        ListFiles();
    }
}

第一次ListFiles()调用成功。我期望第二次ListFiles()调用失败,但它也成功了。如何正确取消连接?似乎WNetCancelConnection2无效。

这也在这里中说明。我想知道是否有人能够使其正常工作。

附上一个网络连接类:

public class NetworkConnection : IDisposable
{
    string _networkName;

    public NetworkConnection(string networkName,
        NetworkCredential credentials)
    {
        _networkName = networkName;

        var netResource = new NetResource()
        {
            Scope = ResourceScope.GlobalNetwork,
            ResourceType = ResourceType.Disk,
            DisplayType = ResourceDisplaytype.Share,
            RemoteName = networkName
        };

        var userName = string.IsNullOrEmpty(credentials.Domain)
            ? credentials.UserName
            : string.Format(@"{0}\{1}", credentials.Domain, credentials.UserName);

        var result = WNetAddConnection2(
            netResource,
            credentials.Password,
            userName,
            0);

        if (result != 0)
        {
            throw new Win32Exception(result, "Error connecting to remote share");
        }
    }

    ~NetworkConnection()
    {
        Dispose(false);
    }

    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
        WNetCancelConnection2(_networkName, 0, true);
    }

    [DllImport("mpr.dll")]
    private static extern int WNetAddConnection2(NetResource netResource,
        string password, string username, int flags);

    [DllImport("mpr.dll")]
    private static extern int WNetCancelConnection2(string name, int flags,
        bool force);
}

[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
    public ResourceScope Scope;
    public ResourceType ResourceType;
    public ResourceDisplaytype DisplayType;
    public int Usage;
    public string LocalName;
    public string RemoteName;
    public string Comment;
    public string Provider;
}

public enum ResourceScope : int
{
    Connected = 1,
    GlobalNetwork,
    Remembered,
    Recent,
    Context
};

public enum ResourceType : int
{
    Any = 0,
    Disk = 1,
    Print = 2,
    Reserved = 8,
}

public enum ResourceDisplaytype : int
{
    Generic = 0x0,
    Domain = 0x01,
    Server = 0x02,
    Share = 0x03,
    File = 0x04,
    Group = 0x05,
    Network = 0x06,
    Root = 0x07,
    Shareadmin = 0x08,
    Directory = 0x09,
    Tree = 0x0a,
    Ndscontainer = 0x0b
}
2个回答

2

我遇到了类似的问题。在代码中等待60秒后,网络连接确实被关闭了。Paulik是正确的,凭据被缓存在某个地方,这导致我的下一个使用不同凭据的连接失败。然而,如果我使用WNetCancelConnection2(pathname, flag, false)关闭连接并等待60秒,我的新凭据将正常工作。不知道原因,但它只是有效。


1
我遇到了同样的问题,不幸的是,没有找到合适的解决方案。我只能描述我的研究结果。
这种方法在幕后使用“net use /delete”命令。它可以正常工作并删除网络连接。您可以通过在命令窗口中调用“net use”来检查。连接已关闭,但是凭据仍然被缓存到某个地方。
所以这个问题的主要原因是缓存的凭据。您可以尝试在运行窗口中调用“control keymgr.dll”在凭据管理器中查找它们,但在我的情况下它们没有显示出来。凭据可能被explorer.exe缓存,因此您可以尝试打开任务管理器并重新启动它。
摆脱它们的唯一可靠方法是使用“net stop”“net start”命令重启Workstation Service

我正在 Windows 7 上进行测试,并发现在断开连接后几分钟后,共享资源将无法从资源管理器中访问。目前我会继续使用模拟登录,因为它没有这个问题。 - TheLegendaryCopyCoder

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接