Desktop Functions:

   Smart Device Functions:

Show Recent Changes
Subscribe (RSS)
Misc. Pages
Helpful Tools
Suggested Reading
Website TODO List
Support Forum
Download Visual Studio Add-In

Terms of Use
Privacy Policy
NetShareAdd (netapi32)
Adds a new network share to either a local or remote server

C# Signature:

    private static extern uint NetShareAdd(
        [MarshalAs(UnmanagedType.LPWStr)] string strServer,
        Int32 dwLevel,
        ref SHARE_INFO_502 buf,
        out uint parm_err

VB Signature:

Declare Unicode Function NetShareAdd Lib "netapi32.dll" _
            (ByVal servername As String, _
            ByVal level As Integer, _
            ByRef buf As SHARE_INFO_2, _
            ByRef parm_err As Integer) As Integer

C# User-Defined Types:

        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_SPECIAL = 0x80000000,

        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;


VB Def and Sample code added by RACKLEY

Tips & Tricks:

Please add some!

C# Sample Code:

    string shareName = "testshare";
    string shareDesc = "This is a test share kekelar2000";
    string path = @"C:\MyShareDirectory"; // do not append comma, it'll fail

    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 = path;
    info.shi502_passwd = null;        // ignored for user-level security
    info.shi502_reserved = 0;
    info.shi502_security_descriptor = IntPtr.Zero;

    uint error = 0;
    uint result = NetShareAdd(server, 502, ref info, out error);

VB.NET Sample Code:

Dim shi2 As SHARE_INFO_2

' Populate the structure with information
shi2.shi2_netname = sharename"      ' share name
shi2.shi2_type = STYPE_DISKTREE     ' disk drive
shi2.shi2_remark = ""           ' share comment
shi2.shi2_permissions = 0
shi2.shi2_max_uses = -1        ' unlimited    
shi2.shi2_current_uses = 0
shi2.shi2_path = "c:\myshare"
shi2.shi2_passwd = Nothing      ' no password

retval = NetShareAdd(ServerName, 2, shi2, Nothing)

Alternative Managed API:

Do you know one? Please contribute it!

NetShareAdd on MSDN

Please edit this page!

Do you have...

  • helpful tips or sample code to share for using this API in managed code?
  • corrections to the existing content?
  • variations of the signature you want to share?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).

Access directly from VS:
Terms of Use
Find References
Show Printable Version