1. By executing the command, there will be a CMD window flashing through:
public void Get_share (string remotepath, String LocalPath, string Username, string password)
{
Process.Start ("NET", "use" +localpath+ "" +remotepath+ "" +password+ "/user:" +username);
}
public void Break_share (string localpath)
{
Process.Start ("NET", "use" +localpath+ "/delete");
}
2. By calling the window's API function:
[DllImport ("Mpr.dll")]
public static extern int wnetaddconnection2a (Netresource [] lpnetresource, String Lppassword, string lpusername, int dwfla GS);
[DllImport ("Mpr.dll")]
public static extern int wnetcancelconnection2a (string sharename,int dwflags,int fforce);
public int Getshare (string remotepath,string localpath,string username,string password)
{
Try
{
Netresource [] share_driver = new Netresource[1];
Share_driver[0].dwtype = 1;
Share_driver[0].lplocalname = LocalPath;
Share_driver[0].lpremotename = RemotePath;
Breakshare (LocalPath);
int ret = WNETADDCONNECTION2A (share_driver, password, username, 1);
return ret;
}
catch (Exception error)
{
throw new Exception (error. message);
}
}
public void Breakshare (string localpath)
{
int ret= WNETCANCELCONNECTION2A (LocalPath, 1, 1);
}