我創建了一個名爲(WindowStartup.EXE)的示例應用程序,顯示計算機名稱,操作系統版本和用戶登錄。此應用程序還包含啓動機器時的自動運行行爲。請參閱下面的代碼。代碼寫在C#如何刪除我的電腦中的自動運行
private void InfoWindow_Load(object sender, EventArgs e)
{
lblMachineName.Text = Environment.MachineName.ToString();
lblOSVersion.Text = Environment.OSVersion.ToString();
lblUserlogged.Text = Environment.UserName.ToString();
this.Top = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;
this.Left = Screen.PrimaryScreen.WorkingArea.Right - this.Width;
if (StartUp()) StartUpSystem();
}
private bool StartUp()
{
bool retVal = false;
if (File.Exists(Application.StartupPath + "\\SystemFile.txt"))
{
//read text file if content is true
Stream file = new FileStream(Application.StartupPath + "\\SystemFile.txt", FileMode.Open, FileAccess.Read);
StreamReader reader = new StreamReader(file);
string content = reader.ReadToEnd();
if (content == "true") retVal = true;
}
return retVal;
}
private void StartUpSystem()
{
RegistryKey regApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (IsStartupItem())
{
//--> Add the value in the registry so that the application runs at startup
regApp.SetValue("WindowStartup.EXE", Application.ExecutablePath.ToString());
}
}
private bool IsStartupItem()
{
// The path to the key where Windows looks for startup applications
RegistryKey regApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (regApp.GetValue("WindowStartup.EXE") == null)
// The value doesn't exist, the application is not set to run at startup
return false;
else
// The value exists, the application is set to run at startup
return true;
}
在創建安裝程序並將其安裝到我的機器中後,它運行時沒有錯誤。但是在卸載這個示例應用程序時,每次啓動我的機器時它都會彈出。
我會嘗試下面的代碼,請從註冊表中的值,但似乎它不工作
private void StartUpSystem()
{
RegistryKey regApp = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (!IsStartupItem())
{
//--> Remove the value from the registry so that the application doesn't start
regApp.DeleteValue("WindowStartup.EXE", false);
}
}
誰能幫我上我如何通過編程將其刪除?
我應該在哪裏把這個代碼?它是附加代碼還是替換代碼? – Bryan
現在檢查,更新我的答案 – Damith
當您設置註冊表值給完整路徑,如「HKEY_CURRENT_USER \ Software \ ... – Damith