Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / Visual Basic

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 184.3K   5.6K   43   36
by using this Method you can Easly Share Folders using C# Programming.

Screenshot - fig2.jpg

Introduction

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 :

C#
//
// 
private static void QshareFolder(string FolderPath, string ShareName, string Description)
{
try{
    // 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:

Reset-Administrator-Pass.asp

History

Version 1.0, By: Qasem Heirani Nobari

License

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


Written By
Team Leader
Iran (Islamic Republic of) Iran (Islamic Republic of)
Ghasem - Nobari
Qasem*AT*SQNco.com

Comments and Discussions

 
QuestionHow to Unshare a Shared Folder Pin
Abhinay_Bandaru7-Jun-18 2:47
Abhinay_Bandaru7-Jun-18 2:47 
QuestionOK - but it's not all Pin
Tomasz Bąk24-Aug-14 8:53
Tomasz Bąk24-Aug-14 8:53 
GeneralThanks Pin
Member 1088098812-Jun-14 8:08
Member 1088098812-Jun-14 8:08 
Questionmodify-description property Pin
Member 1075602817-Apr-14 1:31
Member 1075602817-Apr-14 1:31 
GeneralMy vote of 5 Pin
Amir Mohammad Nasrollahi9-Aug-13 20:46
professionalAmir Mohammad Nasrollahi9-Aug-13 20:46 
GeneralBravo! Pin
bahman0110-Jul-13 21:22
bahman0110-Jul-13 21:22 
BugReturnValue is 9 when FolderPath has trailing back slash Pin
Member 843421321-Dec-12 2:43
Member 843421321-Dec-12 2:43 
GeneralMy vote of 4 Pin
Burak Tunçbilek9-Jul-12 2:56
Burak Tunçbilek9-Jul-12 2:56 
Questionتشكر خيلي به كارم اومد Pin
ehsan_wwe20-Sep-11 5:25
ehsan_wwe20-Sep-11 5:25 
AnswerRe: تشكر خيلي به كارم اومد Pin
Amir Mohammad Nasrollahi9-Aug-13 20:46
professionalAmir Mohammad Nasrollahi9-Aug-13 20:46 
GeneralWhat do the return values mean? Pin
Bernhard Hiller17-Jun-11 2:24
Bernhard Hiller17-Jun-11 2:24 
GeneralRe: What do the return values mean? Pin
Member 1212827431-Jan-17 2:23
Member 1212827431-Jan-17 2:23 
Generalممنون Pin
ali foroughi23-Apr-11 23:15
ali foroughi23-Apr-11 23:15 
AnswerRe: ممنون Pin
Amir Mohammad Nasrollahi9-Aug-13 20:48
professionalAmir Mohammad Nasrollahi9-Aug-13 20:48 
GeneralMy vote of 4 Pin
sds192012-Dec-10 0:34
sds192012-Dec-10 0:34 
GeneralMy vote of 5 Pin
dekmeaw10-Oct-10 20:49
dekmeaw10-Oct-10 20:49 
GeneralThanks.. And please try this tool.. Pin
Geek139-Jun-10 20:28
Geek139-Jun-10 20:28 
QuestionRights Pin
Wamuti2-Jun-10 23:03
Wamuti2-Jun-10 23:03 
AnswerRe: Rights Pin
devvvy18-May-11 19:50
devvvy18-May-11 19:50 
AnswerRe: Rights Pin
Michael900027-Jul-11 22:46
Michael900027-Jul-11 22:46 
Here's a description on how to set permissions. All you have to do is reverse the process.
Programmatically Configuring Permissions on a Share[^]
GeneralThanks mate! Some useful additions... Pin
Geek1311-May-10 0:38
Geek1311-May-10 0:38 
GeneralNice! Pin
doung3030-Mar-09 20:31
doung3030-Mar-09 20:31 
GeneralPrevious Shared Folder Pin
Sally Marlino12-Jul-08 8:56
Sally Marlino12-Jul-08 8:56 
GeneralCache settings Pin
Member 25942017-Mar-08 9:55
Member 25942017-Mar-08 9:55 
GeneralExample code to set permissions after sharing Pin
RandyStimpson25-Oct-07 10:48
RandyStimpson25-Oct-07 10:48 

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.