2016-04-23 41 views
0

我已經創建了一個程序,我將它作爲發佈版本構建到exe中。當你雙擊exe文件時,程序應該將自己添加到啓動中,所以每次啓動pc時,程序都會運行。儘管沒有錯誤,但它並沒有將其添加到啓動中。該計劃也按預期運行。將程序添加到啓動

這是處理程序添加到啓動代碼:

public static void AddApplicationToStartup() 
{ 
    using(RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) 
    { 
     key.SetValue("WindowsProcesses", "\"" + Application.ExecutablePath + "\""); 
    } 

} 

...這個代碼是主要的「綱要」類裏面寫。

+1

你在'key.SetValue'上的錯誤檢查在哪裏? WindowsProcesses存在嗎?如果是這樣,你的價值是否增加了? (原來,你的錯誤在OpenSubKey上檢查?) –

+2

不要這樣做。這對於一個程序來說是不好的行爲。 –

+0

這是一篇很好的CodePlex文章,介紹如何從C#中完成此任務。文章看起來很直截了當,但基本上使用Windows DLL來創建快捷方式。 http://www.codeproject.com/Articles/146757/Add-Remove-Startup-Folder-Shortcut-to-Your-App –

回答

0

添加應用程序的路徑這樣

public static void AddApplicationToStartup() 
     { 

      using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true)) 
      { 
       key.SetValue("WindowsProcesses", Application.ExecutablePath.ToString()); 
      } 

     } 
+0

ok編輯正確,謝謝 – Mostafiz

+0

第三行缺少一個括號在結束,但這段代碼給了我錯誤不能從'Microsoft.Win32.RegistryKey'轉換爲'字符串' –

+0

@OliverDungey我已更新我的代碼,希望這一次將工作並不會拋出任何異常 – Mostafiz

-1

我用這對我的程序和它完美對我很好。它採用可執行文件並在啓動註冊表所在的系統註冊表中添加對其的引用。在這種情況下,我的可執行文件被命名爲「Lemon Soda.exe」。

string fileName = @"\Lemon Soda.exe"; 
string targetPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData); 

RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true); 
registryKey.SetValue("Lemon Soda", targetPath + fileName); 
registryKey.Close(); 
+0

給了我沒有錯誤,但沒有解決這個問題。 –

相關問題