zoukankan      html  css  js  c++  java
  • C# map network drive sample

    reference:http://www.eggheadcafe.com/community/csharp/2/64575/map-network-drive.aspx
    Project property: platform target must be Any CPU!! Must  on x64!! don't know the reason.

    if x86, can map success, but don't appear in windows explorer, so must set to Any CPU ,map success ,can appear in windows explorer. 

    namespace WpfMapHelper
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct NETRESOURCEA
        {
            public int dwScope;
            public int dwType;
            public int dwDisplayType;
            public int dwUsage;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpLocalName;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpRemoteName;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpComment;
            [MarshalAs(UnmanagedType.LPStr)]
            public string lpProvider;
            public override String ToString()
            {
                String str = "LocalName: " + lpLocalName + " RemoteName: " + lpRemoteName
                    + " Comment: " + lpComment + " lpProvider: " + lpProvider;
                return (str);
            }
        }
    
    
    
     
        public static class NetworkDrive
        {
            [DllImport("mpr.dll")]
            public static extern int WNetAddConnection2A(
                [MarshalAs(UnmanagedType.LPArray)] NETRESOURCEA[] lpNetResource,
                [MarshalAs(UnmanagedType.LPStr)] string lpPassword,
                [MarshalAs(UnmanagedType.LPStr)] string UserName,
                int dwFlags);
    
            public static void test()
            {
                NETRESOURCEA[] n = new NETRESOURCEA[1];
                n[0] = new NETRESOURCEA();
                n[0].dwScope = 2;
                n[0].dwType = 0x1;
                n[0].dwDisplayType = 3;
                n[0].dwUsage = 1;
                int dwFlags = 0x00000001;
                n[0].lpLocalName = @"z:";
                n[0].lpRemoteName = @"\\192.168.82.133\public";
                n[0].lpProvider = null;
                Console.WriteLine(n[0]);
                int res = WNetAddConnection2A(n, null, null, dwFlags);
                if (res != 0)
                    throw new System.ComponentModel.Win32Exception(res);
                Console.WriteLine("WNetAddConnection3 returned : " + res);
                Console.WriteLine(n[0]);
            }
        }
    
    }
  • 相关阅读:
    DS博客作业05--查找
    DS博客作业04--图
    DS博客作业03--树
    栈和队列
    第六次作业
    第五次作业
    第四次作业
    第三次作业
    java任务
    第三周-自主学习任务-面向对象基础与类的识别
  • 原文地址:https://www.cnblogs.com/xiaokang088/p/2611144.html
Copyright © 2011-2022 走看看