簡體   English   中英

我在通過使用C#編程共享文件夾時遇到問題?

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

這是我的代碼,它共享該文件夾,但是當我要訪問它時無法正常工作,它顯示需要訪問被拒絕的幫助,

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

您必須向共享文件夾添加權限。 這篇文章使用WMI和Microsoft .Net將權限添加到共享文件夾中詳細說明了這些步驟。

摘錄自

要將權限分配給用戶,需要完成以下操作

  1. 掌握共享文件夾對象的設置並提取其安全描述符。
  2. 從安全描述符中提取訪問控制列表(ACL)。
  3. 掌握用戶帳戶對象並提取其安全描述符。
  4. 使用其安全描述符為用戶創建Windows Trustee對象。
  5. 使用受信者對象創建訪問控制項(ACE)。
  6. 將訪問控制項添加到訪問控制列表。
  7. 將列表分配回該文件夾的安全描述符
  8. 將安全描述符重新分配給共享文件夾。

返回值

返回下表中的值之一或任何其他值以指示錯誤。

0 –成功

2 –訪問被拒絕

8 –未知故障

9 –無效名稱

10 –無效級別

21 –無效參數

22 –重復共享

23 –重定向路徑

24 –未知設備或目錄

25 –找不到網名

您從哪里訪問共享文件夾? 如果是從另一台計算機訪問,請確保已授予該文件夾對您要從其訪問的計算機的讀取特權。希望這對您有所幫助...

謝謝,拉姆

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM