public static bool LoginShareFolder(string desPath, string userName = "******", string password = "") { if (desPath.IsEmptyOrWhileSpace()) { throw new ArgumentException("指定路径错误", "desPath"); } NetResource nr; nr.dwDisplayType = ResourceDisplayType.RESOURCEDISPLAYTYPE_GENERIC; nr.dwScope = ResourceEnumerationScope.RESOURCE_GLOBALNET; nr.dwType = ResourceType.RESOURCETYPE_DISK; nr.dwUsage = ResourceUsage.RESOURCEUSAGE_CONNECTABLE; nr.lpComment = " "; nr.lpLocalName = "z: "; nr.lpProvider = " "; nr.lpRemoteName = desPath; var retCode = CopyFileByNet.WNetAddConnection2(ref nr, password, userName, ConnectionOption.CONNECT_INTERACTIVE); if (retCode == WNetAddConnection2ReturnCode.NO_ERROR) { return(true); } else { throw new Exception(@"登录错误,错误代码为:" + retCode); } }
public static bool LogoutShareFolder(string shareFolderPath) { if (CopyFileByNet.WNetCancelConnection2(shareFolderPath, 0, true) == WNetCancelConnection2ReturnCode.NO_ERROR) { return(true); } else { return(false); } }