2016-04-25 61 views
0

我正在努力獲取文件夾的權限,然後在C#控制檯應用程序中訪問,文件夾的路徑可能是ftp本地.Same正在我的機器上工作,但在生產中部署時服務器其開始投擲錯誤System.InvalidOperationException: Method failed with unexpected error code 64Got System.InvalidOperationException:方法失敗,出現意外的錯誤代碼64

確切的錯誤接收代碼

System.InvalidOperationException: Method failed with unexpected error code 64. 
at System.Security.AccessControl.NativeObjectSecurity.CreateInternal(ResourceType resourceType, Boolean isContainer, String name, SafeHandle handle, AccessControlSections includeSections, Boolean createByName, ExceptionFromErrorCode exceptionFromErrorCode, Object exceptionContext) 
at System.Security.AccessControl.FileSystemSecurity..ctor(Boolean isContainer, String name, AccessControlSections includeSections, Boolean isDirectory) 
at System.Security.AccessControl.DirectorySecurity..ctor(String name, AccessControlSections includeSections) 
at UploadData.FolderManager.IsConfiguredFolderAccessible(String path, Folder folder) 

完整的代碼拋出的錯誤是

private static void IsConfiguredFolderAccessible(string path, Folder folder) 
    { 
     // If the file can be opened for exclusive access it means that the file 
     // is no longer locked by another process. 
     try 
     { 
      if (!Directory.Exists(path)) 
      { 
       LogHelper.Log(string.Format("Folder does not exist on given path {0}. Please re-create folder, grant permission and re-start the UPC utility. ", folder.Path), LogHelper.LogLevel.Error); 
       MailComponent.SendMail(string.Format("Folder does not exist on given path {0}. Please re-create folder, grant permission and re-start the UPC utility.", folder.Path), "Folder does not exist"); 
       return; 
      } 
      else 
      { 
       var accessControlList = Directory.GetAccessControl(path); 

       if (accessControlList == null) 
       { 
        LogHelper.Log(string.Format("AccessControlList on Folder {0} are not defined", folder.ToString()), LogHelper.LogLevel.Error); 
        MailComponent.SendMail(folder.ToString(), "AccessControlList on Folder are not defined"); 
       } 
       var accessRules = accessControlList.GetAccessRules(true, true, 
              typeof(System.Security.Principal.SecurityIdentifier)); 

       if (accessRules == null) 
       { 
        LogHelper.Log(string.Format("AccessRules on Folder {0} are not defined", folder.ToString()), LogHelper.LogLevel.Error); 
        MailComponent.SendMail(folder.ToString(), "AccessRules on Folder are not defined"); 
       } 
       foreach (FileSystemAccessRule rule in accessRules) 
       { 
        if ((FileSystemRights.Write & rule.FileSystemRights) != FileSystemRights.Write) 
         continue; 

        if (rule.AccessControlType == AccessControlType.Deny) 
        { 
         LogHelper.Log(string.Format("Access permission denied on Folder {0}", path), LogHelper.LogLevel.Error); 
         MailComponent.SendMail(folder.ToString(), string.Format("Access permission denied on Folder {0}", path)); 
        } 
       } 
      } 
     } 
     catch (PrivilegeNotHeldException pv) 
     { 
      LogHelper.Log(string.Format("Access permission denied on Folder {0}, Error detail : {1}", path, pv.ToString()), LogHelper.LogLevel.Error); 
      MailComponent.SendMail(pv.ToString(), string.Format("Access permission denied on Folder {0}", path)); 
      throw pv; 
     } 
     catch (IOException io) 
     { 
      LogHelper.Log(string.Format("Folder does not exist on given path {0}, Error detail : {1}", path, io.ToString()), LogHelper.LogLevel.Error); 
      MailComponent.SendMail(io.ToString(), string.Format("Folder does not exist on given path {0}.Please re-create folder, grant permission and re-start the UPC utility.", path)); 
      throw io; 
     } 
     catch (Exception ex) 
     { 
      LogHelper.Log(string.Format("General error occured on Folder {0}, Error detail : {1}", path, ex.ToString()), LogHelper.LogLevel.Error); 
      MailComponent.SendMail(ex.ToString(), "General error occured"); 
      throw ex; 
     } 
    } 
+0

看看[this](http://www.novell.com/support/kb/doc.php?id=7006001)。我希望這會幫助你。 –

回答

0

通過原生的Windows GetSecurityInfo()函數的調用產生的錯誤。你可以在NativeObjectSecurity類的源代碼中看到這個。錯誤64或0x40的十六進制具有winerror.h以下定義:

ERROR_NETNAME_DELETED

指定的網絡名不再可用。

因此,您的問題很可能與通過網絡訪問文件夾相關。

+0

那麼,我怎麼能管理這個錯誤,理想情況下它應該被捕獲並記錄在日誌文件中。 –

+0

@NeerajDubey:你的代碼中有很多異常處理。展開並捕獲'InvalidOperationException'。如果你特別想處理「錯誤64」,你將不得不解析異常信息。在鏈接的參考源中,您可以看到如何拋出異常。但是,根據.NET源代碼的具體情況,將會在您的代碼與您不能控制的內容之間產生緊密耦合。 –

相關問題