我试图连接共享(假设\服务器\文件夹),以我的本地设备X:WNetAddConnection2返回错误1200 - 本地名称是有效
[DllImport("Mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int WNetAddConnection2(
[In] NetResource lpNetResource,
string lpPassword,
string lpUsername,
int flags
);
public static bool Connect(string remoteName, string localName, bool persistent) {
if (!IsLocalPathValid(localName)) return false;
var r = new NetResource
{
dwScope = ResourceScope.RESOURCE_GLOBALNET,
dwType = ResourceType.RESOURCETYPE_ANY,
dwDisplayType = ResourceDisplayType.RESOURCEDISPLAYTYPE_SHARE,
dwUsage = ResourceUsage.RESOURCEUSAGE_CONNECTABLE,
lpRemoteName = remoteName,
lpLocalName = localName
};
return WNetAddConnection2(r, null, null, persistent ? 1 : 0) == 0;
}
[StructLayout(LayoutKind.Sequential)]
public class NetResource {
public ResourceScope dwScope;
public ResourceType dwType;
public ResourceDisplayType dwDisplayType;
public ResourceUsage dwUsage;
public string lpLocalName;
public string lpRemoteName;
public string lpComment;
public string lpProvider;
}
当调用
Connect(@"\\server\folder", "X:", true);
该函数只是返回false - 错误说1200(BAD_DEVICE)。 的NETRESOURCE看起来是这样的:
lpRemoteName = "\\\\server\\folder";
lpProvider = null;
lpLocalName = "X:";
lpComment = null;
dwUsage = Connectable;
dwType = Any;
dwScope = GlobalNet;
dwDisplayType = Share;
我已经与几个片段(PInvoke的)把我看不出有什么差别检查。也许你可以解开这个谜......
EDIT1
如果您不提供本地名称,地图驱动器是否能正常工作?即使'localName'成为一个空字符串。这可能会提供一些线索,看是否还有其他问题。错误代码1200意味着你的“X:\”有一些不正确的地方 - 是否有任何额外的空间(请参阅 - https://stackoverflow.com/questions/31137879/excel-to-sharepoint-mapnetworkdrive-error) – Subbu
不幸的是 - 即使没有指定本地驱动器(空字符串),错误1200仍然存在。查看我的Edit1了解更多NetResource的详细信息。 (我不得不清除服务器名称,对不起。 – C4p741nZ