This is herman's Blog!

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
1.通过执行命令,会有cmd窗口一闪而过:

  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.通过调用WINDOW的API函数:

  [DllImport("mpr.dll")]
  public static extern int WNetAddConnection2A(NETRESOURCE [] lpNetResource, string lpPassword, string lpUserName, int dwFlags);
  [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);
  }

posted on 2005-05-16 18:13  This is herman's Blog!  阅读(491)  评论(0)    收藏  举报