// 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); } }
[DllImport("mpr.dll")] private static extern int WNetAddConnection2A(ref StructNetResource pstNetRes, string psPassword, string psUsername, int piFlags);