c# - WNetAddConnection2 和错误 1219 - 自动断开连接?

标签 c# .net

当我尝试调用 WNetAddConnection2 到我已经有 session 的机器时遇到问题。这是预期的,因为您只能使用一组凭据连接到网络资源。我想要做的是捕获这种情况并自动调用 WNetCancelConnection2 以断开所有 现有连接,然后重试 WNetAddConnection2 调用。当我运行以下代码时,我收到了这些日志消息:

DEBUG - WNetAddConnection2 returned 1219
DEBUG - Multiple credentials detected, disconnecting all current sessions
DEBUG - WNetCancelConnection2 returned 0
DEBUG - WNetAddConnection2 returned 1219

如果我在 WNetCancelConnection 中将 dwFlags 设置为 CONNECT_UPDATE_PROFILE,我会收到这些日志消息:

DEBUG - WNetAddConnection2 returned 1219
DEBUG - Multiple credentials detected, disconnecting all current sessions
DEBUG - WNetCancelConnection2 returned 2250
DEBUG - WNetAddConnection2 returned 1219

这是我的来源,感谢所有帮助!

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

int result = WNetAddConnection2(netResource, credentials.Password, credentials.UserName, 0);

log.Debug("WNetAddConnection2 returned " + result);

if (result == 1219)
{
    log.Debug("Multiple credentials detected, disconnecting all current sessions");

    result = WNetCancelConnection2(networkName, 0, true);
    log.Debug("WNetCancelConnection2 returned " + result);

    result = WNetAddConnection2(netResource, credentials.Password, credentials.UserName, 0);
    log.Debug("WNetAddConnection2 returned " + result);
}

最佳答案

这个问题还存在还是已经解决了?
我遇到了同样的失败,因为我已经打开了与我想要连接的资源的连接。这些连接在启动时由我们的 Windows 网络域的登录脚本自动打开。所以我使用“net use”来断开它们(所有连接到目标计算机)。之后效果很好。

这意味着它不是您的代码中的错误,而是 Windows 网络中的问题。 顺便说一句:无论如何你都应该使用“net use”来检查你的代码是否成功,而不仅仅是相信调试消息。
这是错误代码的链接:http://msdn.microsoft.com/en-us/library/windows/desktop/ms681381%28v=vs.85%29.aspx

关于c# - WNetAddConnection2 和错误 1219 - 自动断开连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9085586/

相关文章:

c# - 当只有一个具有给定名称的方法时,Visual Studio 指出存在不明确的调用

.net - Magento Paypal Express 附加信息字段

c# - 使用 Kendo 菜单打开多个窗口

c# - 在 UWP C# 中不使用 MediaComposition 从 SoftwareBitmap 或 Direct3DSurface 创建视频文件

c# - 创建一组文件的打包二进制表示?

c# - 验证当前经过身份验证的 Windows 用户是否具有委派权限

.net - 在哪里可以下载适用于我的构建服务器的 .NET 4.5 多目标包?

jQuery 调用 Controller 未命中断点

c# - 将 string[] 转换为其关联的枚举数组

c# - Linq to SQL 不保存名为 'Value' 的属性