c# 调用WNetAddConnection2连接windows共享,
NETRESOURCE2 myNetResource = new NETRESOURCE2();
myNetResource.dwScope = 2; //2:RESOURCE_GLOBALNET
myNetResource.dwType = 1; //1:RESOURCETYPE_ANY
myNetResource.dwDisplayType = 3; //3:RESOURCEDISPLAYTYPE_GENERIC
myNetResource.dwUsage = 1; //1: RESOURCEUSAGE_CONNECTABLE
myNetResource.LocalName = null;
myNetResource.RemoteName = remotePath;
myNetResource.Provider = null;
uint nret = WNetAddConnection2(myNetResource, password, username, 0);
return nret == (uint)ERROR_ID.ERROR_SUCCESS;
有时返回 nret =1219,ERROR_SESSION_CREDENTIAL_CONFLICT ,表示本地已有共享服务器已有连接存在。
有个办法,是假设这种情况也是成功的,修改最后一行:
return nret == (uint)ERROR_ID.ERROR_SUCCESS || nret == (uint)ERROR_ID.ERROR_SESSION_CREDENTIAL_CONFLICT;
此时读写共享会用已有的连接。
若已有的连接会话权限和你要使用的账户一致的话,是没问题,但若不一致,比如没有写的权限,则会出现写文件失败,权限不足之类的。
目前的做法是:
1、net use \SERVER /delete 删除连接
2、通过控制面板,清理凭据缓存
3、重启机子;
参考资料:
https://serverfault.com/questions/773082/net-use-error-code-1219
网友评论