C # MAP network drive sample

Source: Internet
Author: User

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]);        }    }}

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.