SIGN UP MEMBER LOGIN:    
ARTICLE

Create Shared Folder Using ManagementClass - C#

Posted by Manikavelu Velayutham Articles | C# Language February 10, 2011
How to create a shared folder with required permission using the ManagementClass in C#.
Reader Level:

Before getting into the code, let's discuss why we need to do this programmatically rather than manually. This kind of functionality is usually required when we are performing an automated deployment process, where we need to create a shared folder for certain user accounts.

C# Code Using ManagementClass

        public
void ShareFolderPermission(string FolderPath, string ShareName, string Description)
        {
            try
            {
                // Calling Win32_Share class to create a shared folder
                ManagementClass managementClass = new ManagementClass("Win32_Share"); 
                // Get the parameter for the Create Method for the folder
                ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");
                ManagementBaseObject outParams; 
                // Assigning the values to the parameters
                inParams["Description"] = Description;
                inParams["Name"] = ShareName;
                inParams["Path"] = FolderPath;
                inParams["Type"] = 0x0; 
                // Finally Invoke the Create Method to do the process
                outParams = managementClass.InvokeMethod("Create", inParams, null); 
                // Validation done here to check sharing is done or not
                if ((uint)(outParams.Properties["ReturnValue"].Value) != 0)
                    MessageBox.Show("Folder might be already in share or unable to share the directory");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            } 
        }

Code Explanation

  1. ManagementClass

It is a WMI class which is generally used to access the WMI data through the ManagementPath that we pass through the constructor of the ManagementClass.

E.g.:-  ManagementClass managementClass = new ManagementClass("Win32_Share");
 

  1. ManagementBaseObject

It contains the basic elements of the management object. It serves as a more specific management object classes.

E.g.:- ManagementBaseObject inParams = managementClass.GetMethodParameters("Create");

Execution

Call the above function like this

ShareFolderPermissions(@"D:\ Test", "Test", "TestApplication");

Once executed, go to the above specified path, right click the folder and view the properties of it. Go to Sharing tab you can see the result like this.

share1.gif

That's the explanation about the code used here. Rest of the code is understandable.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor