Methods for mapping other shared directories to native site characters in C #

Source: Internet
Author: User

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);
  }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.