Click here to Skip to main content
14,929,999 members
Articles / Programming Languages / C#
Posted 27 May 2003


41 bookmarked

How to create a file share using .NET framework

Rate me:
Please Sign up or sign in to vote.
4.13/5 (19 votes)
27 May 20031 min read
How to create a file share using NetApi with PInvoke in Microsoft .NET framework


Title of this article says how to create a file share using .NET framework. But if you really get down to bottom of it, there is no class or method in .NET framework that accomplishes this task. This is the time when we take help from PInvoke to call unmanaged Win32 API. The NetApi in Win32 platform provides a rich set of functions that allows us to do network management.


Network Management API provides following functions to manage file shares.

  • NetShareAdd
  • NetShareDel
  • NetShareCheck
  • NetShareGetInfo
  • NetShareSetInfo

The names of these APIs are very self explanatory. They do what their names suggest. In this article we will show how you can use NetShareAdd API to create a new file share on an existing folder in a machine. Following code is a complete example of how you can use this API with PInvoke in .NET framework to create a share.

There are couple of important points in this code that we would like to highlight.

  • SHARE_INFO_502 structure that contains the share information requires that all strings should be Wide Character strings. Therefore it is important that when you define the structure, use MarshalAs attribute to specify that string should be marshalled appropriately as Wide Character type. You can specify the value foe the attribute as UnmanagedType.LPWStr.
  • In SHARE_INFO_502 structure you can specify the permissions by specifying a Security Descriptor that has the required ACL. If you set the value of shi502_security_descriptor variable to 0, then a null DACL is set on the file share meaning that Everybody has full access to this share.


using System;
using System.Diagnostics;
using System.Collections;
using System.Runtime.InteropServices;
using ActiveDs;

namespace ADSI_Net
    class NetApi32
        public enum NetError
            NERR_Success = 0,
            NERR_BASE = 2100,
            NERR_UnknownDevDir = (NERR_BASE + 16),
            NERR_DuplicateShare = (NERR_BASE + 18),
            NERR_BufTooSmall = (NERR_BASE + 23),

        public enum SHARE_TYPE : ulong
            STYPE_DISKTREE = 0,
            STYPE_PRINTQ = 1,
            STYPE_DEVICE = 2,
            STYPE_IPC = 3,
            STYPE_SPECIAL = 0x80000000,

        [ StructLayout( LayoutKind.Sequential )]
        public struct SHARE_INFO_502
            public string shi502_netname;
            public uint shi502_type;
            public string shi502_remark;
            public Int32 shi502_permissions;
            public Int32 shi502_max_uses;
            public Int32 shi502_current_uses;
            public string shi502_path;
            public IntPtr shi502_passwd;
            public Int32 shi502_reserved;
            public IntPtr shi502_security_descriptor;

        public static extern int NetShareAdd(
        string strServer, Int32 dwLevel, IntPtr buf, IntPtr parm_err);

    class AD_ShareUtil
        static void Main(string[] args)
            string strServer = @"HellRaiser";
            string strShareFolder = @"G:\Mp3folder";
            string strShareName = @"MyMP3Share";
            string strShareDesc = @"Share to store MP3 files";
            NetApi32.NetError nRetVal = 0;
            AD_ShareUtil shUtil = new AD_ShareUtil();
            nRetVal = shUtil.CreateShare(strServer, 
              strShareFolder, strShareName, strShareDesc, false);
            if (nRetVal == NetApi32.NetError.NERR_Success)
                Console.WriteLine("Share {0} created", strShareName);
            else if (nRetVal == NetApi32.NetError.NERR_DuplicateShare)
                Console.WriteLine("Share {0} already exists", 

        NetApi32.NetError CreateShare(string strServer,
                                      string strPath,
                                      string strShareName,
                                      string strShareDesc,
                                      bool bAdmin)
            NetApi32.SHARE_INFO_502 shInfo = 
                  new ADSI_Net.NetApi32.SHARE_INFO_502();
            shInfo.shi502_netname = strShareName;
            shInfo.shi502_type = 
            if (bAdmin)
                shInfo.shi502_type = 
                shInfo.shi502_netname += "$";
            shInfo.shi502_permissions = 0;
            shInfo.shi502_path = strPath;
            shInfo.shi502_passwd = IntPtr.Zero;
            shInfo.shi502_remark = strShareDesc;
            shInfo.shi502_max_uses = -1;
            shInfo.shi502_security_descriptor = IntPtr.Zero;

            string strTargetServer = strServer;
            if (strServer.Length != 0)
                strTargetServer = strServer;
                if (strServer[0] != '\\')
                    strTargetServer = "\\\\" + strServer;
            int nRetValue = 0;
            // Call Net API to add the share..
            int nStSize = Marshal.SizeOf(shInfo);
            IntPtr buffer = Marshal.AllocCoTaskMem(nStSize);
            Marshal.StructureToPtr(shInfo, buffer, false);
            nRetValue = NetApi32.NetShareAdd(strTargetServer, 502, 
                    buffer, IntPtr.Zero);
            Marshal.FreeCoTaskMem( buffer );

            return (NetApi32.NetError)nRetValue;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
United States United States
To learn more about us, Please visit us at

Comments and Discussions

QuestionThe type or namespace name 'ActiveDs' could not be found (are you missing a using directive or an assembly reference?) Pin
Member 1025852521-May-20 15:28
MemberMember 1025852521-May-20 15:28 
QuestionCan I include a modified version of this class in an open source project? Pin
JimShermerhorn14-Dec-11 19:53
MemberJimShermerhorn14-Dec-11 19:53 
QuestionHow to set permissions for this example [modified] Pin
GodLessWarrior8-Jun-10 6:17
MemberGodLessWarrior8-Jun-10 6:17 
AnswerRe: How to set permissions for this example Pin
jazper22-Nov-10 2:41
Memberjazper22-Nov-10 2:41 
GeneralHeres an example of how to set the share permissions as well Pin
Chris128927-May-10 5:39
MemberChris128927-May-10 5:39 
QuestionProblem regarding folder on desktop of Vista Machine Pin
Deepali Dhingra5-Dec-07 2:14
professionalDeepali Dhingra5-Dec-07 2:14 
QuestionDelete Share returning 2310 ? Pin
sushilchoudhari24-Nov-04 8:55
Membersushilchoudhari24-Nov-04 8:55 
AnswerRe: Delete Share returning 2310 ? Pin
sushilchoudhari24-Nov-04 15:21
Membersushilchoudhari24-Nov-04 15:21 
AnswerRe: Delete Share returning 2310 ? Pin
Amar Velmala14-Apr-05 3:18
MemberAmar Velmala14-Apr-05 3:18 
GeneralUPDATE for VB.Net Pin
mef52615-Nov-04 15:38
Membermef52615-Nov-04 15:38 
QuestionHow about Macintosh Share? Pin
AVETAR10-Mar-04 17:12
MemberAVETAR10-Mar-04 17:12 
AnswerRe: How about Macintosh Share? Pin
Anonymous10-Nov-04 6:04
MemberAnonymous10-Nov-04 6:04 
GeneralRe: How about Macintosh Share? Pin
AVETAR10-Nov-04 10:02
MemberAVETAR10-Nov-04 10:02 
GeneralRe: How about Macintosh Share? Pin
David Weprin2-Jul-05 6:20
MemberDavid Weprin2-Jul-05 6:20 
GeneralRemoving Shares Pin
jcawley@1-Aug-03 9:40
Memberjcawley@1-Aug-03 9:40 
GeneralRe: Removing Shares Pin
Anonymous9-Sep-03 13:20
MemberAnonymous9-Sep-03 13:20 
GeneralRe: Removing Shares Pin
Anonymous1-Apr-04 2:44
MemberAnonymous1-Apr-04 2:44 
QuestionPermissions? Pin
William Bartholomew2-Jun-03 16:59
MemberWilliam Bartholomew2-Jun-03 16:59 
AnswerRe: Permissions? Pin
Softomatix3-Jun-03 13:11
MemberSoftomatix3-Jun-03 13:11 
GeneralRe: Permissions? Pin
William Bartholomew3-Jun-03 13:17
MemberWilliam Bartholomew3-Jun-03 13:17 
GeneralRe: Permissions? Pin
Matt Wynn28-Nov-03 5:43
MemberMatt Wynn28-Nov-03 5:43 
GeneralRe: Permissions? Pin
Dave Anderson22-Dec-03 13:52
MemberDave Anderson22-Dec-03 13:52 
GeneralRe: Permissions? Pin
mbakirov22-Sep-05 20:17
Membermbakirov22-Sep-05 20:17 
AnswerRe: Permissions? Pin
sid_boss8-Feb-07 1:06
Membersid_boss8-Feb-07 1:06 
GeneralOther .NET ways to create shares... Pin
Focht28-May-03 23:13
MemberFocht28-May-03 23:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.