Click here to Skip to main content
15,124,906 members
Articles / Programming Languages / Visual Basic
Posted 2 May 2007


43 bookmarked

How to Share Windows Folders Using C#

Rate me:
Please Sign up or sign in to vote.
4.27/5 (29 votes)
2 May 2007GPL3
by using this Method you can Easly Share Folders using C# Programming.

Screenshot - fig2.jpg


By using this Method you can Easly Share Folders with in C# Programming.

I'm wrote Advanced Feature in Source Code Like Maximum Connection and ...

Using the code

At the first you have to Add Reference : System.Management

From Visual Studio like This Image:

Screenshot - fig1.jpg

And Then with this Method You Can Easly Share Any Folder You Want :

private static void QshareFolder(string FolderPath, string ShareName, string Description)
    // Create a ManagementClass object

    ManagementClass managementClass = new ManagementClass("Win32_Share");

    // Create ManagementBaseObjects for in and out parameters

    ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");

    ManagementBaseObject outParams;

    // Set the input parameters

    inParams["Description"] = Description;

    inParams["Name"] = ShareName;

    inParams["Path"] = FolderPath;

    inParams["Type"] = 0x0; // Disk Drive

    //Another Type:

    // DISK_DRIVE = 0x0
    // PRINT_QUEUE = 0x1

    // DEVICE = 0x2

    // IPC = 0x3

    // DISK_DRIVE_ADMIN = 0x80000000

    // PRINT_QUEUE_ADMIN = 0x80000001

    // DEVICE_ADMIN = 0x80000002

    // IPC_ADMIN = 0x8000003

    //inParams["MaximumAllowed"] = int maxConnectionsNum;

    // Invoke the method on the ManagementClass object

    outParams = managementClass.InvokeMethod("Create", inParams, null);

    // Check to see if the method invocation was successful

    if ((uint) (outParams.Properties["ReturnValue"].Value) != 0)


        throw new Exception("Unable to share directory.");


}catch (Exception ex)
//MessageBox.Show(ex.Message, "error!");
Sample Usage: 

QshareFolder("c:\\TestShare","Test Share", "This is a Test Share");

As you see I'm Upload Source code of Sample Program i wrote ,and i hope it's useful to Start!

Points of Interest

I'm using this method in one of my Projects that my Users wants to Share Folder With the others And they don't now How! And i think they will be happy now!

And This Link is an Article About How To Reset Windows Administartor Password:



Version 1.0, By: Qasem Heirani Nobari


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Ghasem Nobari
Team Leader
Iran (Islamic Republic of) Iran (Islamic Republic of)
Ghasem - Nobari

Comments and Discussions

GeneralNice! Pin
doung3030-Mar-09 21:31
Memberdoung3030-Mar-09 21:31 
GeneralPrevious Shared Folder Pin
Sally Marlino12-Jul-08 9:56
MemberSally Marlino12-Jul-08 9:56 
GeneralCache settings Pin
Member 25942017-Mar-08 10:55
MemberMember 25942017-Mar-08 10:55 
GeneralExample code to set permissions after sharing Pin
RandyStimpson25-Oct-07 11:48
MemberRandyStimpson25-Oct-07 11:48 
GeneralRe: Example code to set permissions after sharing Pin
ionymous30-Mar-09 6:20
Memberionymous30-Mar-09 6:20 
GeneralRe: Example code to set permissions after sharing Pin
devvvy18-May-11 19:03
Memberdevvvy18-May-11 19:03 
QuestionPermissions Pin
aagirre23-Aug-07 6:09
Memberaagirre23-Aug-07 6:09 
QuestionError creating share Pin
Andromeda Shun23-Jul-07 0:26
MemberAndromeda Shun23-Jul-07 0:26 
When using your code (or my own code also using WMI) creating a share fails in some cases with error code 9 (illegal name). I cannot tell the reason why this error occurs. I can reproduce this error when I use a certain share name and a certain folder. When I use a different folder sometimes it works and sometimes it doesn't. When I use a different share name sometimes it works and sometimes it doesn't. And it doesn't matter if the folder existed before running the application or if it was created by the application. Under these circumstances this method of sharing folders (WMI) cannot be used in productive software!

Any idea? Or is there an alternative way, using native .NET? With unmanaged C++ I could use NetShareAdd, an SDK function... Perhaps there is a .NET aquivalent? Confused | :confused:

Best regards,
Andromeda Shun
GeneralGood Job, but .... Pin
senpai3219-Jun-07 5:30
Membersenpai3219-Jun-07 5:30 
GeneralCool Pin
Elliott Keith11-May-07 11:34
MemberElliott Keith11-May-07 11:34 
GeneralRe: Cool Pin
Ghasem Nobari11-May-07 20:39
MemberGhasem Nobari11-May-07 20:39 
GeneralCreate Share & Set Permissions Pin
thund3rstruck7-May-07 6:36
Memberthund3rstruck7-May-07 6:36 
GeneralTry-Catch Pin
Steve Hansen3-May-07 0:41
MemberSteve Hansen3-May-07 0:41 
GeneralRe: Try-Catch Pin
Ghasem Nobari3-May-07 0:48
MemberGhasem Nobari3-May-07 0:48 
GeneralThanks! Pin
milangine2-May-07 12:15
Membermilangine2-May-07 12:15 

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.