老D

我是一个粗人
  博客园  :: 首页  :: 新随笔  :: 管理

连接远程服务器共享

Posted on 2007-08-25 14:02  老D  阅读(676)  评论(0编辑  收藏  举报
  1    public class NetWork
  2    {
  3        [StructLayout(LayoutKind.Sequential)]
  4        public class NetResource
  5        {
  6            Create net use sturcture
 69
 70            public RESOURCE_SCOPE dwScope;
 71            public RESOURCE_TYPE dwType;
 72            public RESOURCE_DISPLAYTYPE dwDisplayType;
 73            public RESOURCE_USAGE dwUsage;
 74            public string LocalName;
 75            public string RemoteName;
 76            public string Comment;
 77            public string Provider;
 78        }

 79
 80        ///   
 81        /// mpr.dll  
 82        ///   

 83        [DllImport("mpr.dll", CharSet = CharSet.Ansi)]
 84        public static extern int WNetAddConnection2A(NetResource netResource,
 85                                                     String password,
 86                                                     String Username,
 87                                                     int Flag);
 88
 89
 90        ///   
 91        /// Create net use.  
 92        ///   

 93        public int CreateNetConnection(string RemoteName, string Password, string UserName)
 94        {
 95            NetResource myNetResource = new NetResource();
 96            myNetResource.dwScope = NetResource.RESOURCE_SCOPE.RESOURCE_GLOBALNET;
 97            myNetResource.dwType = NetResource.RESOURCE_TYPE.RESOURCETYPE_ANY;
 98            myNetResource.dwDisplayType = NetResource.RESOURCE_DISPLAYTYPE.RESOURCEDISPLAYTYPE_GENERIC;
 99            myNetResource.dwUsage = NetResource.RESOURCE_USAGE.RESOURCEUSAGE_CONNECTABLE;
100            //myNetResource.LocalName = "Z:"; // Mapping to local dirver  
101            myNetResource.RemoteName = RemoteName; // Remote host IP or host name.  
102            myNetResource.Provider = null;
103            return WNetAddConnection2A(myNetResource, Password, UserName, 0);
104        }

105
106    }