One Community, One World
Programming Shared Files and Folders
by Ahmed Waly  on Jan 17 2007 12:00AM
  PageViews 10950   |    Add to Fav   |     Send to Friend   |    Download Code
Learn how to develop applications that can share, unshared, or get shared files and folders information.

Resources Used:
Microsoft Visual Studio 2005

Let's Start...
Start by creating a new windows application, and name it for example "IHShares". 

Now, add new listview control to the form, and set its properties as in the figure. It will be used to display all the shared folders in the machine.  

Figure 1

Add two buttons to the form, one for share delete and the other to create a new share.  

Now, let's code...  
Add a new reference to the application, System.Management; then add the following lines to the "using” section; we will be using these libraries in our application:

using System; 
using System.Drawing; 
using System.Collections; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Data; 
using System.Management; 
using System.IO; 
using System.Text;   

In the global declarations of the form, add these declarations to be used:   

// Object to query the WMI Win32_Share API for shared files... 
ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from win32_share"); 
ManagementBaseObject outParams; ManagementClass mc = new ManagementClass("Win32_Share");    
//for local shares // ManagementClass mc = new ManagementClass("\\\\IHMachineName\\root\\cimv2:Win32_Share");   
//for remote shares 

In the previous declarations, we queried the Win32_Share WMI API to get all the shared files.  

Now add the following method to the code, it will be used to retrieve all the shared and display them in the listview control: 

#region GetShares()         
    public void GetShares()         

                        foreach (ManagementObject share in searcher.Get())                 
                                string type;                     
                                System.Windows.Forms.ListViewItem Item1 = new System.Windows.Forms.ListViewItem();
                                System.Windows.Forms.ListViewItem.ListViewSubItem SubItem1 = new System.Windows.Forms.ListViewItem.ListViewSubItem();
                                System.Windows.Forms.ListViewItem.ListViewSubItem SubItem2 = new System.Windows.Forms.ListViewItem.ListViewSubItem();
                                System.Windows.Forms.ListViewItem.ListViewSubItem SubItem3 = new System.Windows.Forms.ListViewItem.ListViewSubItem();
                                Item1.Text = share["Name"].ToString();  //getting share name
                                SubItem1.Text = share["Path"].ToString();  //getting share path
                                SubItem3.Text = share["Caption"].ToString();    //getting share description

                                // Analyzing Share type, translate it from No. to Name
                                type = share["Type"].ToString();
                                if (type == "0")
                                    SubItem2.Text = "Disk Drive";
                                else if (type == "1")
                                    SubItem2.Text = "Print Queue";
                                else if (type == "2")
                                    SubItem2.Text = "Device";
                                else if (type == "3")
                                    SubItem2.Text = "IPC";
                                else if (type == "2147483648")
                                    SubItem2.Text = "Disk Drive Admin";
                                else if (type == "2147483649")
                                    SubItem2.Text = "Print Queue Admin";
                                else if (type == "2147483650")
                                    SubItem2.Text = "Device Admin";
                                    SubItem2.Text = "IPC Admin";


                        catch (Exception ex)


Now, call this method in the Form Load event like this:

private void Form1_Load(object sender, EventArgs e)         

And run now to see the results.  

Now, let's code the delete method. 

#region DeleteShares()        

     public void DeleteShares()         
                    foreach (ManagementObject mo in mc.GetInstances())                 
                            int selNdx = this.lvwShares.SelectedIndices[0]; //gets the index of selected item                     
                            string Name = lvwShares.Items[selNdx].SubItems[0].Text;   //getting the share name of selected item.                     
                            if (mo["Name"].ToString() == Name)                         
                                outParams = mo.InvokeMethod("Delete", null, null);                 

                    if (Convert.ToInt32(outParams.Properties["ReturnValue"].Value) == 0)
                         MessageBox.Show("Deleted successfully");                 
            catch (Exception ex)             


private void cmdDelete_Click(object sender, EventArgs e)         

To here, we are able to view, and delete any shared files or folders we need.  

But to create a new share, we will add a new form to the application, like in the figure. 

Figure 2

And call the new form in the New Share button: 
private void cmdNew_Click(object sender, EventArgs e)         
            new create().ShowDialog();         

Now, we create the method used to share the folders:    
#region ShareDir()             

    public void ShareDir()             
                //for creating share on remote computer use:                         
                //    ManagementScope wmiScope;                         
                //    wmiScope = new ManagementScope("\\" & Server & "\\root\\cimv2");                         
                //    wmiScope.Connect();                         
                //    wmiShare = new ManagementClass(wmiScope, new ManagementPath("Win32_Share"), null);                           
                ManagementClass wmiShare;                         
                ManagementBaseObject inParams;                         
                ManagementBaseObject outParams;                           
                wmiShare = new ManagementClass("Win32_Share");                         
                inParams = wmiShare.GetMethodParameters("Create");                         
                inParams["Path"] = txtPath.Text;    //setting the path                         
                inParams["Name"] = txtName.Text;    //setting the share name                         
                inParams["Type"] = 0;                     //setting share type to (Disk Drive)                         
                inParams["MaximumAllowed"] = null//setting a property                         
                inParams["Description"] = txtDesc.Text;   //setting share description                         
                outParams = wmiShare.InvokeMethod("Create", inParams, null);                   
        catch (Exception ex)                   

Now we are ready to start sharing folders, you only need to test the application.

Using WMI APIs with C#, we are able to perform many actions that are not implemented by default in the .NET libraries.