Torna al Thread
public class NetworkDrive
{
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2A(ref structNetResource pstNetRes, string psPassword, string psUsername, int piFlags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2A(string psName, int piFlags, int pfForce);
[DllImport("mpr.dll")]
private static extern int WNetRestoreConnectionW(int phWnd, string psLocalDrive);
[DllImport("kernel32.dll")]
private static extern int GetDriveTypeA(string ndrive);
[StructLayout(LayoutKind.Sequential)]
private struct structNetResource
{
public int iScope;
public int iType;
public int iDisplayType;
public int iUsage;
public string sLocalName;
public string sRemoteName;
public string sComment;
public string sProvider;
}
private const int RESOURCETYPE_DISK = 0x1;
private const int CONNECT_INTERACTIVE = 0x00000008;
private const int CONNECT_PROMPT = 0x00000010;
private const int CONNECT_UPDATE_PROFILE = 0x00000001;
private const int CONNECT_REDIRECT = 0x00000080;
private const int CONNECT_COMMANDLINE = 0x00000800;
private const int CONNECT_CMD_SAVECRED = 0x00001000;
private bool lf_SaveCredentials = false;
private bool lf_Persistent = false;
private bool lf_Force = false;
private bool ls_PromptForCredentials = false;
private string ls_Drive = "s:";
private string ls_ShareName = "\\\\Computer\\C$";
public bool SaveCredentials
{
get { return (lf_SaveCredentials); }
set { lf_SaveCredentials = value; }
}
public bool Persistent
{
get { return (lf_Persistent); }
set { lf_Persistent = value; }
}
public bool Force
{
get { return (lf_Force); }
set { lf_Force = value; }
}
public bool PromptForCredentials
{
get { return (ls_PromptForCredentials); }
set { ls_PromptForCredentials = value; }
}
public string LocalDrive
{
get { return (ls_Drive); }
set
{
if (value.Length >= 1)
ls_Drive = value.Substring(0, 1) + ":";
else ls_Drive = "";
}
}
public string ShareName
{
get { return (ls_ShareName); }
set { ls_ShareName = value; }
}
public void MapDrive() { zMapDrive(null, null); }
public void MapDrive(string Password) { zMapDrive(null, Password); }
public void MapDrive(string Username, string Password) { zMapDrive(Username, Password); }
public void UnMapDrive() { zUnMapDrive(this.lf_Force); }
public void RestoreDrives() { zRestoreDrive(); }
private void zMapDrive(string psUsername, string psPassword)
{
//create struct data
structNetResource stNetRes = new structNetResource();
stNetRes.iType = RESOURCETYPE_DISK;
stNetRes.sRemoteName = ls_ShareName;
LocalDrive = this.Freedrive();
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 (lf_Force) try { zUnMapDrive(true); } catch { }
int i = WNetAddConnection2A(ref stNetRes, psPassword, psUsername, iFlags);
if (i > 0) { throw new System.ComponentModel.Win32Exception(i); }
}
private void zUnMapDrive(bool pfForce)
{
int iFlags = 0;
if (lf_Persistent) { iFlags += CONNECT_UPDATE_PROFILE; }
pfForce = true;
int i = WNetCancelConnection2A(ls_Drive, iFlags, Convert.ToInt32(pfForce));
if (i > 0) { throw new System.ComponentModel.Win32Exception(i); }
}
private void zRestoreDrive()
{
int i = WNetRestoreConnectionW(0, null);
if (i > 0) { throw new System.ComponentModel.Win32Exception(i); }
}
private string Freedrive()
{
int DriveNum;
int FirstDrive;
string NextDrive;
DriveNum = 2;
do
{
DriveNum = DriveNum + 1;
NextDrive = (Convert.ToChar(DriveNum + 65)).ToString() + ":\\";
FirstDrive = GetDriveTypeA(NextDrive);
} while (FirstDrive != 1);
return (NextDrive);
}
}
#endregion