2012-12-29 18 views
0

我有一個項目,我已經添加了一個安裝程序項目。我跟着這(custom installerc#應用程序自動啓動窗口7

我添加了一個自定義安裝程序類。其中一個目的是在任何用戶登錄時將該程序添加到註冊表以進行自動啓動。我以管理員身份運行visual studio(在visual studio的頂部說明 - 注意:在計算機管理中,我不是管理員)。我的筆記本電腦也使用了一個名爲powerbroker的應用程序。要安裝應用程序,請右鍵單擊並選擇運行提升。從閱讀其他職位運行提升是不一樣的管理員,其中可能存在的問題。

無論如何,問題是: 在Visual Studio中,沒有錯誤產生(代碼運行正常)添加密鑰(我寫了一個單獨的應用程序來測試這個,但密鑰沒有被寫入 - 我不明白爲什麼

當我把代碼在我的安裝程序並運行升高不會引發錯誤,關鍵是沒有寫任何 - 至少,如果它出錯了,並回滾安裝....

我做嘗試爲當前用戶設置密鑰,而且工作正常,但對我無用......

ia也創建了一個本地用戶,其中誰是管理員的成員,也沒有訪問權限。

要總結一下我試圖搞清楚的是: 我怎麼能扔的註冊表寫入失敗的錯誤,並回滾安裝(記住代碼目前不會拋出低於下高架privaleges一個錯誤,但犯規實際工作)

有沒有解決這個問題?

感謝 DAMO

C#安裝程序類代碼

using System; 
using System.Diagnostics; 
using System.Windows.Forms; 
using System.Collections; 
using System.ComponentModel; 
using System.Configuration.Install; 
using System.Reflection; 
using System.IO; 
using Microsoft.Win32; 



namespace OffLine.Installer 
{ 
    // Taken from:http://msdn2.microsoft.com/en-us/library/ 
    // system.configuration.configurationmanager.aspx 
    // Set 'RunInstaller' attribute to true. 

    [RunInstaller(true)] 
    public class InstallerClass : System.Configuration.Install.Installer 
    { 
     public InstallerClass() 
      : base() 
     { 
      // Attach the 'Committed' event. 
      this.Committed += new InstallEventHandler(MyInstaller_Committed); 
      // Attach the 'Committing' event. 
      this.Committing += new InstallEventHandler(MyInstaller_Committing); 
     } 

     // Event handler for 'Committing' event. 
     private void MyInstaller_Committing(object sender, InstallEventArgs e) 
     { 
      // **** Beta Only **** Set program to autostart 
      try 
      { 

       RegistryKey add = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 

       add.SetValue("FactoryAuditEventNotification", "\"" + Application.ExecutablePath.ToString() + "\""); 

      } 
      catch (Exception ex) 
      { 
       MessageBox.Show(ex.Message.ToString());    

      } 

     } 

     // Event handler for 'Committed' event. 
     private void MyInstaller_Committed(object sender, InstallEventArgs e) 
     { 
      try 
      { 
       Directory.SetCurrentDirectory(Path.GetDirectoryName 
       (Assembly.GetExecutingAssembly().Location)); 
       Process.Start(Path.GetDirectoryName(
        Assembly.GetExecutingAssembly().Location) + "\\FactoryAuditEventNotification.exe"); 
      } 
      catch 
      { 
       // Do nothing... 

      } 
     } 

     // Override the 'Install' method. 
     public override void Install(IDictionary savedState) 
     { 
      base.Install(savedState); 
     } 

     // Override the 'Commit' method. 
     public override void Commit(IDictionary savedState) 
     { 
      base.Commit(savedState); 
     } 

     // Override the 'Rollback' method. 
     public override void Rollback(IDictionary savedState) 
     { 
      base.Rollback(savedState); 
     } 
    } 
} 
+2

查看註冊表項HKLM \ .. \ Run的權限。打開註冊表。點擊鍵。在編輯菜單上,單擊權限。嘗試使用[procmon](http://technet.microsoft.com/en-us/sysinternals/bb896645.aspx)來跟蹤對hklm \ .. \ run鍵的訪問。 – sergmat

+1

這是編寫註冊表值的大量代碼。你有沒有考慮過使用註冊表? –

+0

@ christopher painter - 是的,我剛剛嘗試過 - 我添加了一個字符串值「[TARGETDIR] myapp.exe」到位置HKPU \ SOFTWARE \ Microsoft \ Windows \ Currentversion \ Run,但這也不起作用 - 它實際上添加此值爲HKPU \ SOFTWARE \ WOW6432node \ Microsoft \ Windows \ Currentversion \ Run – user1438082

回答