C#實現網際網路共用

來源:互聯網
上載者:User
using System;using System.Runtime.InteropServices;namespace ConApp{    class RemoteShareDir    {        [DllImport("Netapi32.dll",CharSet=CharSet.Unicode)]        private static extern uint NetShareAdd(            [MarshalAs(UnmanagedType.LPWStr)] string strServer,            Int32 dwLevel,            ref SHARE_INFO_502 buf,            out uint parm_err        );        [DllImport("Netapi32.dll",CharSet= CharSet.Unicode )]        private static extern uint NetShareDel(string host, string shareName, int reserved);        public RemoteShareDir()        {        }        public uint SetShareDir(string host, string dir, string shareName, string shareDesc)        {            SHARE_INFO_502 info = new SHARE_INFO_502();            info.shi502_netname = shareName;            info.shi502_type = SHARE_TYPE.STYPE_DISKTREE;            info.shi502_remark = shareDesc;            info.shi502_permissions = 0;    // ignored for user-level security            info.shi502_max_uses = -1;            info.shi502_current_uses = 0;    // ignored for set            info.shi502_path = dir;            info.shi502_passwd = null;        // ignored for user-level security            info.shi502_reserved = 0;            info.shi502_security_descriptor = IntPtr.Zero;            uint error = 0;            return NetShareAdd(host, 502, ref info, out error);        }        public uint DeleteShareDir(string host, string shareName)        {            return NetShareDel(host, shareName, 0);        }        private enum NetError : uint        {            NERR_Success = 0,            NERR_BASE = 2100,            NERR_UnknownDevDir = (NERR_BASE + 16),            NERR_DuplicateShare = (NERR_BASE + 18),            NERR_BufTooSmall = (NERR_BASE + 23),        }        private enum SHARE_TYPE : uint        {            STYPE_DISKTREE = 0,            STYPE_PRINTQ = 1,            STYPE_DEVICE = 2,            STYPE_IPC = 3,            STYPE_TEMPORARY = 0x40000000,            STYPE_SPECIAL = 0x80000000,        }        [StructLayout(LayoutKind.Sequential)]        private struct SHARE_INFO_502        {            [MarshalAs(UnmanagedType.LPWStr)]            public string shi502_netname;            public SHARE_TYPE shi502_type;            [MarshalAs(UnmanagedType.LPWStr)]            public string shi502_remark;            public Int32 shi502_permissions;            public Int32 shi502_max_uses;            public Int32 shi502_current_uses;            [MarshalAs(UnmanagedType.LPWStr)]            public string shi502_path;            [MarshalAs(UnmanagedType.LPWStr)]            public string shi502_passwd;            public Int32 shi502_reserved;            public IntPtr shi502_security_descriptor;        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.