Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello Friends,

I need to create a folder and share with other network users. I have to do this by code. I am able to create this in windows XP without any issue with the help of C#. But in Vista and Windows 7, I have to do some extra settings manually. How can I make it working in case of Windows 7 and Vista.

Below is the code I am using:
public void CreateSharedDirectory()
        {
            string DirPath = Environment.CurrentDirectory;
            try
            {

                Directory.CreateDirectory(DirPath + "\\TestSharedFolder");
                
                DirectoryInfo dInfo = new DirectoryInfo(DirPath + "\\TestSharedFolder");

                DirectorySecurity dSecurity = dInfo.GetAccessControl();
                // Add the FileSystemAccessRule to the security settings.  
                dSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
                dSecurity.AddAccessRule(new FileSystemAccessRule("NETWORK SERVICE", FileSystemRights.FullControl, AccessControlType.Allow));
                // Set the new access settings. NETWORK SERVICE
                dInfo.SetAccessControl(dSecurity);

                /////////////////////////////////////////////////////////////////////////////////////////////////
                ManagementClass management = new ManagementClass("Win32_Share");
                ManagementBaseObject input = management.GetMethodParameters("Create");
                ManagementBaseObject output;

                input["Description"] = "TestSharedFolder";
                input["Name"] = "TestSharedFolder";
                input["Path"] = DirPath + "\\TestSharedFolder";
                input["Type"] = 0x0;
                input["Access"] = null;

                output = management.InvokeMethod("Create", input, null);

                if ((uint)(output.Properties["ReturnValue"].Value) != 0)
                {
                    throw new Exception("Unable To Share The Directory");
                }
                else
                {
                    Console.Write("Directory Successfully Shared");
                    
                }
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
            }
        }


Please suggest a solution.


Thanks,
Jagjot
Posted
Updated 14-Jun-11 3:43am
v2
Comments
Sergey Alexandrovich Kryukov 14-Jun-11 10:41am    
Solution for what? What's the problem? How do you want to share a directory and why?
--SA
Jagjot Singh 15-Jun-11 1:21am    
These are basic sharing options which are working in XP only. I have to give advance settings manually in case of Windows7 and Vista. I want to do it programmatically.
Sergey Alexandrovich Kryukov 17-Jun-11 23:14pm    
True. I see. Sorry, I don't have Windows 7 installation now to take a look at this.
--SA
ClockEndGooner 2-Jul-11 23:22pm    
Hi, Jagjot;

Have you looked at http://social.msdn.microsoft.com/Forums/en-US/windowssdk/thread/de213b61-dc7e-4f33-acdb-893aa96837fa, which discusses how to create a shared folder in Windows 7 and then how to programmatically set the permissions on the shared folder?

I hope this was of help...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900