开发者

i have problem with sharing a folder through programming using c#?

here is my code it shares the folder but that does not work correctly when i want to access it , it shows access denied help required,

private static void ShareFolder(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", inParam开发者_StackOverflows, null);
            // Check to see if the method invocation was successful

            if ((uint)(outParams.Properties["ReturnValue"].Value) != 0)
            {
                throw new Exception("Unable to share directory. Because Directory is already shared or directory not exist");
            }//end if

        }//end try
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "error!");
        }//end catch
    }//End Method


You have to add permissions to the shared folders. This post Adding Permissions to a shared folder using WMI and Microsoft .Net explains the steps in detail.

Excerpt from the post

To assign permission to the user, the following needs to be done

  1. Get hold of the Shared folder object’s setting and extract its security descriptor.
  2. Extract Access Control List (ACL) from the security descriptor.
  3. Get hold of the user account object and extract its security descriptor.
  4. Create a Windows Trustee object for the user using its security descriptor.
  5. Create an Access Control Entry (ACE) using the Trustee object.
  6. Add Access Control Entry to Access Control List.
  7. Assign List back to Security Descriptor for the folder
  8. Reassign security descriptor to the shared folder.


Return Values

Returns one of the values in the following table or any other value to indicate an error.

0 – Success

2 – Access denied

8 – Unknown failure

9 – Invalid name

10 – Invalid level

21 – Invalid parameter

22 – Duplicate share

23 – Redirected path

24 – Unknown device or directory

25 – Net name not found


Where are you accessing the shared folder from? If from another computer, make sure you have given read privileges on that folder to the computer that you are accessing it from.. Hope this helps...

Thanks, Ram

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜