0

我已将所有启动程序添加到列表框中。当我选择项目并单击按钮时,如何删除选定的注册表项?

列表框代码:

    private void starting()
    {
        RegistryKey HKCU = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
        {
            foreach (string Programs in HKCU.GetValueNames())
            {
                startupinfo.Items.Add(Programs);
            }
            HKCU.Close();
        }
        RegistryKey HKLM = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run");
        {
            foreach (string HKLMPrograms in HKLM.GetValueNames())
            {
                startupinfo.Items.Add(HKLMPrograms);
            }
            HKLM.Close();
        }

这是我可以逐个文件删除的启动文件夹:(感谢 Rikki-B 帮助我)

    private void readfiles()
    {
        string startfolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);

        var files = Directory.GetFiles(startfolder).Where(name => !name.EndsWith(".ini"));

        foreach (string file in files)
        {
            startupinfo.Items.Add(System.IO.Path.GetFileName(file));
            startupinfoDict.Add(System.IO.Path.GetFileName(file), file);
        }
    }

这是按钮:

    private void DisableBtn_Click(object sender, RoutedEventArgs e)
    {
        if (startupinfo.SelectedItem != null)
    {
        string s = startupinfo.SelectedItem.ToString();

        if (startupinfoDict.ContainsKey(s))
        {
            File.Delete(startupinfoDict[s]);
        }
    }
}

列表框的外观:

1

4

1 回答 1

1

试试这个。

private void DisableBtn_Click(object sender, RoutedEventArgs e)
{
    if (startupinfo.SelectedItem != null)
    {
        string s = startupinfo.SelectedItem.ToString();

        if (startupinfoDict.ContainsKey(s))
        {
            try
            {
                File.Delete(startupinfoDict[s]);
            }
            catch
            {
                //errors are here
            }
        }

        string keyName = @"Software\Microsoft\Windows\CurrentVersion\Run";
        using (Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(keyName, true))
        {
            if (key != null)
            {
                try
                {
                    key.DeleteValue(startupinfo.SelectedItem.ToString());
                }
                catch
                {
                    //errors are here
                }
            }
        }
    }
}
于 2013-04-11T01:11:36.243 回答