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]);
            }
        }
    
    }
  • 相关阅读:
    python 日期、时间戳转换
    判断任意数字是否为素数
    linux使用工具记录
    python日志记录-logging模块
    python特性、属性以及私有化
    python 装饰器、内部函数、闭包简单理解
    sql语句操作记录
    virtualBox使用nat模式下ssh连接
    git常用操作
    分布式CAP定理(转)
  • 原文地址:https://www.cnblogs.com/xiaokang088/p/2611144.html
Copyright © 2011-2022 走看看