创建共享连接的封装

public class ShareConnection  
    {  
        public ShareConnection() { }  
 
        [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]  
        private static extern uint WNetAddConnection2(  
         [In] NETRESOURCE lpNetResource,  
         string lpPassword,  
         string lpUsername,  
         uint dwFlags);  
 
 
        [DllImport("Mpr.dll")]  
        private static extern uint WNetCancelConnection2(  
         string lpName,  
         uint dwFlags,  
         bool fForce);  
 
        [StructLayout(LayoutKind.Sequential)]  
        private 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;  
        }  
 
        /// <summary>  
        /// 创建一个映射盘  
        /// </summary>  
        /// <param name="driver">盘符,如"Z:"</param>  
        /// <param name="sharePath">共享路径</param>  
        /// <param name="uName">用户名</param>  
        /// <param name="uPwd">用户密码</param>  
        /// <returns></returns>  
        public static bool CreateShareDriver(string driver, string sharePath, string uName, string uPwd)  
        {  
            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 = driver;  
            myNetResource.RemoteName = sharePath;  
            myNetResource.Provider = null;  
 
            uint nret = WNetAddConnection2(myNetResource, uPwd, uName, 0);  
 
            //注意:如果正确,返回值是0;否则错误。  
            if (nret == 0 || nret == 85)   //85为已经存在的错误码,这里不返回错误  
                return true;  
            else 
                return false;  
        }  
 
        /// <summary>  
        /// 创建一个连接  
        /// </summary>  
        /// <param name="sharePath"></param>  
        /// <param name="uName"></param>  
        /// <param name="uPwd"></param>  
        /// <returns></returns>  
        public static bool ConnectionSharePath(string sharePath, string uName, string uPwd)  
        {  
            return CreateShareDriver("", sharePath, uName, uPwd);  
        }  
 
        /// <summary>  
        /// 删除一个映射盘  
        /// </summary>  
        /// <param name="driver"></param>  
        /// <returns></returns>  
        public static bool DelShareDriver(string driver)  
        {  
            uint nret = WNetCancelConnection2(driver, 1, true);  
            if (nret == 0)  
                return true;  
            else 
                return false;  
        }  
 
        /// <summary>  
        /// 删除一个共享连接  
        /// </summary>  
        /// <param name="connPath">共享连接路径</param>  
        /// <returns></returns>  
        public static bool CancelConnection(string connPath)  
        {  
            return DelShareDriver(connPath);  
        }  
    } 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yls087412/archive/2010/03/29/5426937.aspx

你可能感兴趣的:(职场,休闲,访问网络共享文件夹)