/// <summary> #endregion #region Core functions // Map network drive private void zMapDrive(string psUsername, string psPassword) { //create struct data structNetResource stNetRes = new structNetResource(); stNetRes.iScope = 2; stNetRes.iType = RESOURCETYPE_DISK; stNetRes.iDisplayType = 3; stNetRes.iUsage = 1; stNetRes.sRemoteName = ls_ShareName; stNetRes.sLocalName = ls_Drive; //prepare params int iFlags = 0; if (lf_SaveCredentials) { iFlags += CONNECT_CMD_SAVECRED; } if (lf_Persistent) { iFlags += CONNECT_UPDATE_PROFILE; } if (ls_PromptForCredentials) { iFlags += CONNECT_INTERACTIVE + CONNECT_PROMPT; } if (psUsername == "") { psUsername = null; } if (psPassword == "") { psPassword = null; } //if force, unmap ready for new connection if (lf_Force) { try { zUnMapDrive(true); } catch { } } //call and return int i = WNetAddConnection2A(ref stNetRes, psPassword, psUsername, iFlags); if (i > 0) { throw new System.ComponentModel.Win32Exception(i); } }
/// <summary> /// Connects to remote share with username and password /// </summary> /// <param name="UNCcomputer">Remote Computer</param> /// <param name="username">User Name</param> /// <param name="password">Password</param> /// <returns>nothing, just hooks up to remote share</returns> public static void ConnectToComputer(string UNCcomputer, string username, string password) { structNetResource stNetRes = new structNetResource(); stNetRes.iScope = 2; stNetRes.iType = RESOURCETYPE_DISK; stNetRes.iDisplayType = 3; stNetRes.iUsage = 1; stNetRes.sRemoteName = UNCcomputer; stNetRes.sLocalName = "IRC$"; int iFlags = 0; if (username == "") { username = null; } if (password == "") { password = null; } int result = WNetAddConnection2A(ref stNetRes, password, username, iFlags); }
public int MapDrive(string driveLetter, string user, string password, string networkpath, bool promptForCredentials, bool saveCredentials, bool persistent, bool forceConnection, string description_optional = null) { Log.LogFile.INSTANCE.DebugWriteLine("Call -> " + System.Reflection.MethodBase.GetCurrentMethod()); int i = 1; try { //create struct data structNetResource stNetRes = new structNetResource(); stNetRes.iScope = 2; stNetRes.iType = RESOURCETYPE_DISK; stNetRes.iDisplayType = 3; stNetRes.iUsage = 1; stNetRes.sRemoteName = networkpath; driveLetter = driveLetter.Trim(new char[] { '\\' }); stNetRes.sLocalName = driveLetter; //prepare params int iFlags = 0; if (saveCredentials) { iFlags += CONNECT_CMD_SAVECRED; } //save credentials:no if (persistent) { iFlags += CONNECT_UPDATE_PROFILE; } //persistent:no if (promptForCredentials) { iFlags += CONNECT_INTERACTIVE + CONNECT_PROMPT; } if (user == "") { user = null; } if (password == "") { password = null; } //if force, unmap ready for new connection if (forceConnection) { try { } catch { } } // zUnMapDrive(true); //call and return i = WNetAddConnection2A(ref stNetRes, password, user, iFlags); if (i > 0) { Log.LogFile.INSTANCE.DebugWriteLine("ERROR: Exception thrown in class: " + this.ToString() + ", METHOD: " + System.Reflection.MethodBase.GetCurrentMethod() + ", EXCEPTION INFORMATION: " + new System.ComponentModel.Win32Exception(i).ToString()); } if (i == 0 && description_optional != null) { // Registry Setting RegistryKey rk = Registry.CurrentUser; string subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\MountPoints2\\" + networkpath.Replace("\\", "#"); // I have to use CreateSubKey // (create or open it if already exits), // 'cause OpenSubKey open a subKey as read-only RegistryKey sk1 = rk.CreateSubKey(subKey); sk1.SetValue("_LabelFromDesktopINI", description_optional); } } catch (Exception e) { Log.LogFile.INSTANCE.DebugWriteLine("ERROR: Exception thrown in class: " + this.ToString() + ", METHOD: " + System.Reflection.MethodBase.GetCurrentMethod() + ", EXCEPTION INFORMATION: " + e.ToString()); } return(i); }
[DllImport("mpr.dll")] private static extern int WNetAddConnection2A(ref structNetResource pstNetRes, string psPassword, string psUsername, int piFlags);
// Map network drive private void zMapDrive(string psUsername, string psPassword) { //create struct data structNetResource stNetRes = new structNetResource(); stNetRes.iScope=2; stNetRes.iType=RESOURCETYPE_DISK; stNetRes.iDisplayType=3; stNetRes.iUsage=1; stNetRes.sRemoteName=ls_ShareName; stNetRes.sLocalName=ls_Drive; //prepare params int iFlags=0; if(lf_SaveCredentials){iFlags+=CONNECT_CMD_SAVECRED;} if(lf_Persistent){iFlags+=CONNECT_UPDATE_PROFILE;} if(ls_PromptForCredentials){iFlags+=CONNECT_INTERACTIVE+CONNECT_PROMPT;} if(psUsername==""){psUsername=null;} if(psPassword==""){psPassword=null;} //if force, unmap ready for new connection if(lf_Force){try{zUnMapDrive(true);}catch{}} //call and return int i = WNetAddConnection2A(ref stNetRes, psPassword, psUsername, iFlags); if(i>0){throw new System.ComponentModel.Win32Exception(i);} }
private static extern int WNetAddConnection2A(ref structNetResource pstNetRes, string psPassword, string psUsername, int piFlags);
//public void ShowConnectDialog(Form ParentForm){zDisplayDialog(ParentForm,1);} /// <summary> /// Display windows dialog for mapping a network drive /// </summary> /// <summary> /// Display windows dialog for disconnecting a network drive /// </summary> //public void ShowDisconnectDialog(Form ParentForm){zDisplayDialog(ParentForm,2);} #endregion #region Core functions // Map network drive private void zMapDrive(string psUsername, string psPassword) { #if _PERFORMANCE_LOG_ PerfLog.Enter(psUsername); #endif try { //create struct data var stNetRes = new structNetResource(); stNetRes.iScope = 2; stNetRes.iType = RESOURCETYPE_DISK; stNetRes.iDisplayType = 3; stNetRes.iUsage = 1; stNetRes.sRemoteName = ls_ShareName; stNetRes.sLocalName = ls_Drive; //prepare params int iFlags = 0; if (lf_SaveCredentials) { iFlags += CONNECT_CMD_SAVECRED; } if (lf_Persistent) { iFlags += CONNECT_UPDATE_PROFILE; } if (ls_PromptForCredentials) { iFlags += CONNECT_INTERACTIVE + CONNECT_PROMPT; } if (psUsername == "") { psUsername = null; } if (psPassword == "") { psPassword = null; } //if force, unmap ready for new connection if (lf_Force) { try { zUnMapDrive(true); } catch { } } //call and return int i = WNetAddConnection2A(ref stNetRes, psPassword, psUsername, iFlags); if (i > 0) { throw new Win32Exception(i); } } catch (Exception e) { Logger.Fatal("Unexpected Exception.", e); throw; } #if _PERFORMANCE_LOG_ finally { PerfLog.Exit(); } #endif }
private static extern int WNetAddConnection(ref structNetResource netResStruct, string password, string username, int flags);
/// <summary> /// Map network drive /// </summary> /// <param name="username">Username</param> /// <param name="password">Password</param> private void mapDrive(string username, string password) { // if drive property is set to auto select, collect next free drive if (FindNextFreeDrive) { m_LocalDrive = nextFreeDrive(); if (string.IsNullOrEmpty(m_LocalDrive)) { throw new Exception("Could not find valid free drive name"); } } // create struct data to pass to the api function structNetResource stNetRes = new structNetResource(); stNetRes.Scope = 2; stNetRes.Type = RESOURCETYPE_DISK; stNetRes.DisplayType = 3; stNetRes.Usage = 1; stNetRes.RemoteName = ShareName; stNetRes.LocalDrive = m_LocalDrive; // prepare flags for drive mapping options int iFlags = 0; if (SaveCredentials) { iFlags += CONNECT_CMD_SAVECRED; } if (Persistent) { iFlags += CONNECT_UPDATE_PROFILE; } if (PromptForCredentials) { iFlags += CONNECT_INTERACTIVE + CONNECT_PROMPT; } // prepare username / password params if (username != null && username.Length == 0) { username = null; } if (password != null && password.Length == 0) { password = null; } // if force, unmap ready for new connection if (Force) { try { unMapDrive(); } catch (Exception ex) { Debug.WriteLine(ex); } } // call and return int i = WNetAddConnection(ref stNetRes, password, username, iFlags); if (i > 0) { throw new Win32Exception(i); } }
// map network drive private void mapDrive(string username, string password) { // if drive property is set to auto select, collect next free drive if (_findNextFreeDrive) { _localDrive = nextFreeDrive(); if (_localDrive == null || _localDrive.Length == 0) throw new System.Exception("Could not find valid free drive name"); } // create struct data to pass to the api function structNetResource stNetRes = new structNetResource(); stNetRes.Scope = 2; stNetRes.Type = RESOURCETYPE_DISK; stNetRes.DisplayType = 3; stNetRes.Usage = 1; stNetRes.RemoteName = _shareName; stNetRes.LocalDrive = _localDrive; // prepare flags for drive mapping options int iFlags = 0; if (_saveCredentials) iFlags += CONNECT_CMD_SAVECRED; if (_persistent) iFlags += CONNECT_UPDATE_PROFILE; if (_promptForCredentials) iFlags += CONNECT_INTERACTIVE + CONNECT_PROMPT; // prepare username / password params if (username != null && username.Length == 0) username = null; if (password != null && password.Length == 0) password = null; // if force, unmap ready for new connection if (_force) { try { this.unMapDrive(); } catch { } } // call and return int i = WNetAddConnection(ref stNetRes, password, username, iFlags); if (i > 0) throw new System.ComponentModel.Win32Exception(i); }
private static extern int WNetAddConnection2A(ref structNetResource NetResStruct, string Password, string Username, int Flags);
/// <summary> /// Connects to remote share with username and password /// </summary> /// <param name="UNCcomputer">Remote Computer</param> /// <param name="username">User Name</param> /// <param name="password">Password</param> /// <returns>nothing, just hooks up to remote share</returns> public static void ConnectToComputer(string UNCcomputer, string username, string password) { structNetResource stNetRes = new structNetResource(); stNetRes.iScope = 2; stNetRes.iType = RESOURCETYPE_DISK; stNetRes.iDisplayType = 3; stNetRes.iUsage = 1; stNetRes.sRemoteName = UNCcomputer; stNetRes.sLocalName = "IRC$"; int iFlags = 0; if (username == "") {username = null;} if (password == "") {password = null;} int result = WNetAddConnection2A(ref stNetRes, password, username, iFlags); }