我无法通过使用c#编程来共享文件夹?

我无法通过使用c#编程来共享文件夹?,c#,C#,这是我的代码,它共享文件夹,但在我想要访问它时无法正常工作,它显示访问被拒绝,需要帮助 private static void ShareFolder(string FolderPath, string ShareName, string Description) { try { // Create a ManagementClass object ManagementClass managementClas

这是我的代码,它共享文件夹,但在我想要访问它时无法正常工作,它显示访问被拒绝,需要帮助

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", 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. 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

您从何处访问共享文件夹?如果从另一台计算机访问,请确保您已将该文件夹的读取权限授予访问该文件夹的计算机。。希望这有助于

谢谢,
Ram

您必须向共享文件夹添加权限。这篇文章详细解释了这些步骤

摘自《华盛顿邮报》

要将权限分配给用户,请 需要做以下工作

  • 获取共享文件夹对象的设置并提取其 安全描述符
  • 从安全描述符中提取访问控制列表(ACL)
  • 获取用户帐户对象并提取其安全性 描述符
  • 使用其安全性为用户创建Windows受信者对象 描述符
  • 使用受信者对象创建访问控制条目(ACE)
  • 将访问控制项添加到访问控制列表
  • 将列表分配回文件夹的安全描述符
  • 将安全描述符重新分配给共享文件夹
  • 返回值

    返回下表中的一个值或指示错误的任何其他值

    0–成功

    2–访问被拒绝

    8–未知故障

    9–无效名称

    10–无效级别

    21–无效参数

    22–重复共享

    23–重定向路径

    24–未知设备或目录


    25–未找到网络名称

    我建议您显示ex.ToString()而不是ex.Message。可能还有更多你看不到的信息。