如下代码连接了一个计算机的共享文件,映射成了本机的一个磁盘:
        [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;
        }
        private void button3_Click(object sender, EventArgs e)//连接
        {
            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 = "M:";
            myNetResource.RemoteName = @"\\192.168.1.104\aa";  //aa为共享文件夹的名字
            
            myNetResource.Provider = null;
            uint nret = WNetAddConnection2(myNetResource, "", "TT", 0);   //第二个参数为计算机密码,第三个为计算机名              MessageBox.Show("创建成功~");            
        }        private void button4_Click(object sender, EventArgs e)   //断开
        {
            uint nret = WNetCancelConnection2("M:", 1, true);
            MessageBox.Show("网盘已断开~");
        }现在得到了网络驱动器的M盘,可是现它的名字却为"'(192.168.1.104)' 上的 aa",现想更改它的名字,请问如何修改

解决方案 »

  1.   

    1、WNetAddConnection2(LPNETRESOURCE lp...)该原形是传指针的。你试试把.NET的函数声明改为:
    [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")] 
    public static extern uint WNetAddConnection2( 
                ref NETRESOURCE lpNetResource,                         //<----
                string lpPassword, 
                string lpUsername, 
                uint dwFlags); 
    2、看看nret返回值是否非零,如果非零,查它的错误代码,或贴出来:uint nret = WNetAddConnection2(ref myNetResource, "", "TT", 0);     //<----
      

  2.   

    注册表:
    Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2",true);SetValue("_LabelFromDesktopINI", "我的驱动盘");