2012-07-16 61 views
0

我的應用程序調用一個庫(我沒有控制權)創建一個新的EventLog源並使用EventLog.SourceExists。它引發System.Security.SecurityException: The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security.創建新的事件日誌源

該應用需要讀訪問HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\eventlog\Security。如何將網絡服務權限授予註冊表(以編程方式)?

感謝您的指點。

回答

0

您會收到此錯誤消息,因爲您的「新來源」未註冊,因此您需要管理權限。嘗試在控制檯中以「管理員」身份運行您的APP。

我曾經通過自己添加「Source」來破解「註冊表」,但這可能不太合適。

+0

我的應用程序不需要管理員權限,只需要安全分支的讀訪問權限。 – Sam 2012-07-16 20:05:06

+0

只有擁有管理員或域權限(只讀)的用戶才能使用「設計」安全分支。以編程方式繞過這一點,您可能需要更改「註冊表設置」。 KB附加:http://support.microsoft.com/kb/323076 – 2012-07-16 20:16:02

0

我今天遇到了同樣的問題,對於我的情況(非安裝計劃任務exe),沒有任何WinForms或ASPX的答案似乎可行。所以我這樣做: -

protected void prog_Load(object sender, EventArgs e) 
    { 
     boolean setupComplete = false; 
     try // setting an Event log entry, just to see if we can 
     { 
      logEvent = "prog started"; 
      EventLog.WriteEntry(logSource, logEvent, EventLogEntryType.Information, 0); 
      setupComplete = true; 
     } 
     catch (Exception eLog1) // we can't, so try to fix 
     { 
      try 
      { 
       EventLog.CreateEventSource(logSource, logLog); 
       logEvent = "prog registered for Event Logging"; 
       EventLog.WriteEntry(logSource, logEvent, EventLogEntryType.Information, 0); 
      } 
      catch (Exception eLog2) // aha! we probably lack admin rights to set the registry key 
      { 
       MessageBox.Show("prog needs admin rights the first time it runs", "prog Setup", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      } 
     } 

     // run 
     if (setupComplete == true) 
     { 
      DoTheWork(); 
     } 

     // exit 
     this.Close(); 
    }