zoukankan      html  css  js  c++  java
  • C#里将其他共享目录映射为本地盘符的方法

    C#里将其他共享目录映射为本地盘符的方法

    分享
    from:
    http://gaoxinembed.blog.sohu.com/142397846.html

       

    转载CSDN文章


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

  • 相关阅读:
    一句SQL实现MYSQL的递归查询
    人生不过一个字【Life is but a word】
    VS2008 如何将Release版本设置可以调试的DEBUG版本
    微软 2018 年第一笔收购:文件存储公司 Avere Systems
    设置系统时间
    OpenVZ安装指南,一种操作系统级别的虚拟化技术
    云平台DevOps实践
    路由(Routing)
    Ubuntu命令
    net mvc中angular
  • 原文地址:https://www.cnblogs.com/baishahe/p/2414032.html
Copyright © 2011-2022 走看看