Click here to Skip to main content
6,629,377 members and growing! (21,886 online)
Email Password   helpLost your password?
General Reading » Hardware & System » General     Intermediate License: The GNU General Public License (GPL)

How to Share Windows Folders Using C#

By Qasem Heyrani Nobari

by using this Method you can Easly Share Folders using C# Programming.
C# 2.0, VB 9.0.NET 1.0, .NET 1.1, .NET 2.0, Win2K, WinXP, Win2003, Vista, .NET 3.0VS2005, Dev
Posted:2 May 2007
Views:37,887
Bookmarked:26 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
12 votes for this article.
Popularity: 4.28 Rating: 3.97 out of 5

1

2
2 votes, 16.7%
3
3 votes, 25.0%
4
7 votes, 58.3%
5

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 :

//

// 

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 (GPL)

About the Author

Qasem Heyrani Nobari


Member
Ghasem - HeyraniNobari (Heirani Nobari, Heyrani Nobari)
http://SQNco.com
http://Ajaxdic.com
QHN_PROF*AT*Yahoo.com
Occupation: Software Developer (Senior)
Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular Hardware & System articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
GeneralNice! Pinmemberdoung3021:31 30 Mar '09  
GeneralPrevious Shared Folder PinmemberSally Marlino9:56 12 Jul '08  
GeneralCache settings PinmemberMember 259420110:55 7 Mar '08  
GeneralExample code to set permissions after sharing PinmemberRandyStimpson11:48 25 Oct '07  
GeneralRe: Example code to set permissions after sharing Pinmemberionymous6:20 30 Mar '09  
QuestionPermissions Pinmemberaagirre6:09 23 Aug '07  
QuestionError creating share PinmemberAndromeda Shun0:26 23 Jul '07  
GeneralGood Job, but .... Pinmembersenpai325:30 19 Jun '07  
GeneralCool PinmemberElliott Keith11:34 11 May '07  
GeneralRe: Cool PinmemberQHN_PROF20:39 11 May '07  
GeneralCreate Share & Set Permissions Pinmemberthund3rstruck6:36 7 May '07  
GeneralTry-Catch PinmemberSteve Hansen0:41 3 May '07  
GeneralRe: Try-Catch PinmemberQHN_PROF0:48 3 May '07  
GeneralThanks! Pinmembermilangine12:15 2 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 2 May 2007
Editor:
Copyright 2007 by Qasem Heyrani Nobari
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project