/// <summary> /// Verbindet die zuvor definierte Netzwerkfreigabe. Im Fehlerfall ein das Event NetworkError ausgelöst. /// </summary> /// <returns>Ergebnis des Verbindugsversuchs</returns> public String Connect() { var netResource = new NetworkResource() { _Scope = ResourceScope.GlobalNetwork, _ResourceType = ResourceType.Disk, _DisplayType = ResourceDisplaytype.Share, _RemoteName = this._networkName }; var userName = string.IsNullOrEmpty(this._credentials.Domain) ? this._credentials.UserName : string.Format(@"{0}\{1}", this._credentials.Domain, this._credentials.UserName); var result = WNetAddConnection2( netResource, this._credentials.Password, userName, 0); if (result != 0) { if (this.NetworkError != null) { String Errorhint = ""; if (this.Errorhints.ContainsKey(result.ToString())) { Errorhint += "\r\n" + this.Errorhints[result.ToString()]; } this.NetworkError(this, new ErrorEventArgs("Error connecting to remote share \"" + this._networkName + "\" as user \"" + this._credentials.UserName + "@" + this._credentials.Domain + "\". Errorcode is: " + result.ToString() + Errorhint)); //Wieder einkommentieren //this.NetworkError(this, new ErrorEventArgs("Error connecting to remote share \"" + this._networkName + "\" as user \"" + this._credentials.UserName + "@" + this._credentials.Domain + "\" using Password \"" + this._credentials.Password + "\". Errorcode is: " + result.ToString())); //DEBUG!! //Auskommentieren!! } } return(result.ToString()); }
/// <summary> /// Verbindet die zuvor definierte Netzwerkfreigabe. Im Fehlerfall ein das Event NetworkError ausgelöst. /// </summary> /// <returns>Ergebnis des Verbindugsversuchs</returns> public String Connect(bool forcedisconnectother = false) { var netResource = new NetworkResource() { _Scope = ResourceScope.GlobalNetwork, _ResourceType = ResourceType.Disk, _DisplayType = ResourceDisplaytype.Share, _RemoteName = this._networkName }; var userName = string.IsNullOrEmpty(this._credentials.Domain) ? this._credentials.UserName : string.Format(@"{0}\{1}", this._credentials.Domain, this._credentials.UserName); var result = WNetAddConnection2( netResource, this._credentials.Password, userName, 0); if (result != 0) { if (result == 1219 /*0x4c3*/) { if (forcedisconnectother) { /* * https://docs.microsoft.com/en-us/windows/win32/debug/system-error-codes--1000-1299- * ERROR_SESSION_CREDENTIAL_CONFLICT * 1219 (0x4C3) * Multiple connections to a server or shared resource by the same user, using more than one user name, are not allowed. Disconnect all previous connections to the server or shared resource and try again. * * -> Sadly Windows even in 2020 is still not capable of connecting SMB as multiple-users simultainiously. We may need to close existings connections if we hit tis error code. However this will only we work if no active session is opened in explorer. * We may use: WNetCancelConnection2(networkName, 0, true); for this * -> See https://stackoverflow.com/questions/9085586/wnetaddconnection2-and-error-1219-automatically-disconnect/18700184 */ try { WNetCancelConnection2(this._networkName, 0, true); //Force close an active SMB-Session to that resource } catch { } /* * disconnecttry++; * * if (disconnecttry > 3) * { * System.c * } */ } } if (this.NetworkError != null) { String Errorhint = ""; if (this.Errorhints.ContainsKey(result.ToString())) { Errorhint += "\r\n" + this.Errorhints[result.ToString()]; } this.NetworkError(this, new ErrorEventArgs("Error connecting to remote share \"" + this._networkName + "\" as user \"" + this._credentials.UserName + "@" + this._credentials.Domain + "\". Errorcode is: " + result.ToString() + Errorhint)); //Wieder einkommentieren //this.NetworkError(this, new ErrorEventArgs("Error connecting to remote share \"" + this._networkName + "\" as user \"" + this._credentials.UserName + "@" + this._credentials.Domain + "\" using Password \"" + this._credentials.Password + "\". Errorcode is: " + result.ToString())); //DEBUG!! //Auskommentieren!! } } return(result.ToString()); }
private static extern int WNetAddConnection2(NetworkResource netResource, string password, string username, int flags);