zoukankan      html  css  js  c++  java
  • 如何把网络共享目录映射为本地驱动器

    要映射一个网络目录为本地驱动器,需要调用系统DLLWNetAddConnection2函数来进行添加。

     

    首先,系统函数的申明如下:

    using System.Runtime.InteropServices;

     

        [DllImport("mpr.dll", EntryPoint="WNetAddConnection2")]

        public static extern uint WNetAddConnection2(

    [In] NETRESOURCE lpNetResource,

    string lpPassword,

    string lpUsername,

    uint dwFlags);

     

        [DllImport("Mpr.dll")]

        public static extern uint WNetCancelConnection2(

    string lpName,

    uint dwFlags,

    bool fForce);

     

        [StructLayout(LayoutKind.Sequential)]

        public class NETRESOURCE

        {   

            public int dwScope;   

            public int dwType;

            public int dwDisplayType;   

            public int dwUsage;   

            public string LocalName;   

            public string RemoteName;   

            public string Comment;   

            public string Provider;

        }

        添加映射网络驱动器调用的代码如下:

        NETRESOURCE myNetResource = new NETRESOURCE();       

        myNetResource.dwScope = 2;       //2:RESOURCE_GLOBALNET           

        myNetResource.dwType = 1 ;       //1:RESOURCETYPE_ANY            

        myNetResource.dwDisplayType = 3; //3:RESOURCEDISPLAYTYPE_GENERIC          

        myNetResource.dwUsage = 1;       //1: RESOURCEUSAGE_CONNECTABLE    

        myNetResource.LocalName = "T:";       

        myNetResource.RemoteName = yourNetworkPath;       

        myNetResource.Provider = null;       

     

        uint nret = WNetAddConnection2( myNetResource, pwd, username, 0);

        注意:如果正确,返回值是0;否则错误。

        删除映射网络驱动器调用的代码如下:

        uint nret = WNetCancelConnection2( yourNetDriveName, 1, true);

     

     

     

    首先,要在工程中引用Windows.Management

     

    然后,就是用WMI进行查询,代码如下:

        public enum DRIVE_TYPE:int

        {

            REMOVABLE       = 2,

            LOCALDISK       = 3,

            NETDRIVE            = 4,

            CDROM           = 5,

        }

     

        public ArrayList GetAllNetDriveName()

        {

            //get drive collection

            string strQuery = string.Format( "SELECT * From Win32_LogicalDisk WHERE DriveType = {0}", (int)( DRIVE_TYPE.NETDRIVE) );

     

            ManagementObjectSearcher query = new ManagementObjectSearcher(strQuery);

            ManagementObjectCollection queryCollection = query.Get();

     

            ArrayList arrNetDriveName = new ArrayList();

            foreach ( ManagementObject mo in queryCollection)

                arrNetDriveName.Add( mo["Name"].ToString() );

            return arrNetDriveName;

        }

     

           网络驱动器的盘符已经获得,那么删除,就可以调用原来的方法,参见:

  • 相关阅读:
    文本建模、文本分类相关开源项目推荐(Pytorch实现)
    自然语言推断(NLI)、文本相似度相关开源项目推荐(Pytorch 实现)
    20155312 张竞予 Exp9 Web安全基础
    20155312 张竞予 Exp 8 Web基础
    20155312 张竞予 Exp7 网络欺诈防范
    20155312 张竞予 Exp6 信息搜集与漏洞扫描
    20155312 张竞予 Exp5 MSF基础应用
    20155312 张竞予 Exp4 恶意代码分析
    20155312 张竞予 Exp3 免杀原理与实践
    20155312 张竞予 Exp2 后门原理与实践
  • 原文地址:https://www.cnblogs.com/kenter/p/2122971.html
Copyright © 2011-2022 走看看