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

  • 相关阅读:
    关于String和StringBuilder、StringBuffer的一个简单性能测试
    HTML网页BODY中如何设置背景图拉伸的最有效方法
    JS鼠标事件大全
    去除链接虚线框的推荐方法
    CSS实现文字颠倒旋转效果
    三种方法解决IE6下png透明失效的问题
    js获取节点 dom操作
    IE HACK
    javascript作用域(Scope)
    RGB配色表
  • 原文地址:https://www.cnblogs.com/baishahe/p/2414032.html
Copyright © 2011-2022 走看看