Click here to Skip to main content
Licence GPL3
First Posted 2 May 2007
Views 72,669
Downloads 1,598
Bookmarked 36 times

How to Share Windows Folders Using C#

By | 2 May 2007 | Article
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 :

//
// 
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)

About the Author

Ghasem Heyrani Nobari

Software Developer (Senior)

Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member

Ghasem - Heyrani-Nobari
http://SQNco.com
http://Ajaxdic.com
Qasem*AT*SQNco.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionتشكر خيلي به كارم اومد Pinmemberehsan_wwe5:25 20 Sep '11  
GeneralWhat do the return values mean? PinmemberBernhard Hiller2:24 17 Jun '11  
Generalممنون Pinmemberali foroughi23:15 23 Apr '11  
GeneralMy vote of 4 Pinmembersds19200:34 12 Dec '10  
GeneralMy vote of 5 Pinmemberdekmeaw20:49 10 Oct '10  
GeneralThanks.. And please try this tool.. PinmemberGeek1320:28 9 Jun '10  
QuestionRights PinmemberWamuti23:03 2 Jun '10  
AnswerRe: Rights Pinmemberdevvvy19:50 18 May '11  
AnswerRe: Rights PinmemberMichael900022:46 27 Jul '11  
GeneralThanks mate! Some useful additions... PinmemberGeek130:38 11 May '10  
GeneralNice! Pinmemberdoung3020:31 30 Mar '09  
GeneralPrevious Shared Folder PinmemberSally Marlino8:56 12 Jul '08  
GeneralCache settings PinmemberMember 25942019:55 7 Mar '08  
GeneralExample code to set permissions after sharing PinmemberRandyStimpson10:48 25 Oct '07  
using System.Security.AccessControl;
using System.Security.Principal;
 
...
 
NTAccount account = new NTAccount("Domain Users");
SecurityIdentifier sId = (SecurityIdentifier)account.Translate(typeof SecurityIdentifier));
DirectorySecurity dirSec = new DirectorySecurity();
FileSystemAccessRule accessRule = new FileSystemAccessRule(sId,
FileSystemRights.FullControl, InheritanceFlags.ObjectInherit |
InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow);
dirSec.AddAccessRule(accessRule);
Directory.SetAccessControl("c:\YourSharedFolder", dirSec);

 
---
RandyStimpson.blogspot.com
Bug-Tracking-Guidelines.com

GeneralRe: Example code to set permissions after sharing Pinmemberionymous5:20 30 Mar '09  
GeneralRe: Example code to set permissions after sharing Pinmemberdevvvy18:03 18 May '11  
QuestionPermissions Pinmemberaagirre5:09 23 Aug '07  
QuestionError creating share PinmemberAndromeda Shun23:26 22 Jul '07  
GeneralGood Job, but .... Pinmembersenpai324:30 19 Jun '07  
GeneralCool PinmemberElliott Keith10:34 11 May '07  
GeneralRe: Cool PinmemberQHN_PROF19:39 11 May '07  
GeneralCreate Share & Set Permissions Pinmemberthund3rstruck5:36 7 May '07  
GeneralTry-Catch PinmemberSteve Hansen23:41 2 May '07  
GeneralRe: Try-Catch PinmemberQHN_PROF23:48 2 May '07  
GeneralThanks! Pinmembermilangine11:15 2 May '07  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 2 May 2007
Article Copyright 2007 by Ghasem Heyrani Nobari
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid