C#实现启动项管理的示例代码

.NET
269
0
0
2023-07-04
标签   C#
目录
  • 实践过程
  • 效果
  • 代码

实践过程

效果

代码

public partial class Form : Form
{
    public Form()
    {
        InitializeComponent();
    }

    string[] Machine;
    string[] User;
    private void getMachineInfo()//读取HKEY_LOCAL_MACHINE分支下的启动项
    {
        string[] reginfo = new string[];
        RegistryKey rk= Registry.LocalMachine;
        RegistryKey rk=rk.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
        string[] MachineFiles = rk.GetValueNames();
        Machine = rk.GetValueNames();
        foreach (string name in MachineFiles)
        {
            string registdata;
            RegistryKey rk;
            RegistryKey rk;
            rk = Registry.LocalMachine;
            rk = rk.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
            registdata = rk.GetValue(name).ToString();
            reginfo[] = name;
            reginfo[] = registdata;
            ListViewItem lvi = new ListViewItem(reginfo);
            listView.Items.Add(lvi);
        }
    }

    private void getUserInfo()
    {
        string[] reginfo = new string[];
        RegistryKey rk = Registry.CurrentUser;
        RegistryKey rk = rk.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
        string[] UserFiles = rk.GetValueNames();
        User = rk.GetValueNames();
        foreach (string name in UserFiles)
        {
            string registdata;
            RegistryKey rk;
            RegistryKey rk;
            rk = Registry.CurrentUser;
            rk = rk.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
            registdata = rk.GetValue(name).ToString();
            reginfo[] = name;
            reginfo[] = registdata;
            ListViewItem lvi = new ListViewItem(reginfo);
            listView.Items.Add(lvi);
        }
    }
    private void Form_Load(object sender, EventArgs e)
    {
        getMachineInfo();
        getUserInfo();
    }

    private bool IsMachine(string name)
    {
        bool flag = false;
        for (int i =; i < Machine.Length; i++)
        {
            if (Machine[i] == name)
            {
                flag = true;
            }
        }
        return flag;
    }

    private bool IsUser(string name)
    {
        bool flag = false;
        for (int i =; i <User.Length; i++)
        {
            if (User[i] == name)
            {
                flag = true;
            }
        }
        return flag;
    }

    private void button_Click(object sender, EventArgs e)
    {
        if (listView.Items.Count > 0)
        {
            for (int i =; i < listView1.Items.Count; i++)
            {
                if (listView.Items[i].Checked == true)
                {
                    string name = listView.Items[i].SubItems[0].Text;
                    if (IsMachine(name))
                    {
                        RegistryKey rk;
                        RegistryKey rk;
                        rk = Registry.LocalMachine;
                        rk = rk0.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                        rk.DeleteValue(name);
                    }
                    if (IsUser(name))
                    {
                        RegistryKey rk;
                        RegistryKey rk;
                        rk = Registry.CurrentUser;
                        rk = rk0.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
                        rk.DeleteValue(name);
                    }
                }
            }
            listView.Items.Clear();
            getMachineInfo();
            getUserInfo();
        }
    }

    private void button_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }
}
partial class Form
{
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.listView = new System.Windows.Forms.ListView();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.columnHeader = new System.Windows.Forms.ColumnHeader();
        this.splitContainer = new System.Windows.Forms.SplitContainer();
        this.button = new System.Windows.Forms.Button();
        this.button = new System.Windows.Forms.Button();
        this.splitContainer.Panel1.SuspendLayout();
        this.splitContainer.Panel2.SuspendLayout();
        this.splitContainer.SuspendLayout();
        this.SuspendLayout();
        // 
        // listView
        // 
        this.listView.CheckBoxes = true;
        this.listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
        this.columnHeader,
        this.columnHeader});
        this.listView.Dock = System.Windows.Forms.DockStyle.Fill;
        this.listView.FullRowSelect = true;
        this.listView.GridLines = true;
        this.listView.Location = new System.Drawing.Point(0, 0);
        this.listView.Name = "listView1";
        this.listView.Size = new System.Drawing.Size(482, 210);
        this.listView.TabIndex = 0;
        this.listView.UseCompatibleStateImageBehavior = false;
        this.listView.View = System.Windows.Forms.View.Details;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "启动项";
        this.columnHeader.Width = 147;
        // 
        // columnHeader
        // 
        this.columnHeader.Text = "说明";
        this.columnHeader.Width = 330;
        // 
        // splitContainer
        // 
        this.splitContainer.Dock = System.Windows.Forms.DockStyle.Fill;
        this.splitContainer.Location = new System.Drawing.Point(0, 0);
        this.splitContainer.Name = "splitContainer1";
        this.splitContainer.Orientation = System.Windows.Forms.Orientation.Horizontal;
        // 
        // splitContainer.Panel1
        // 
        this.splitContainer.Panel1.Controls.Add(this.listView1);
        // 
        // splitContainer.Panel2
        // 
        this.splitContainer.Panel2.Controls.Add(this.button2);
        this.splitContainer.Panel2.Controls.Add(this.button1);
        this.splitContainer.Size = new System.Drawing.Size(482, 245);
        this.splitContainer.SplitterDistance = 210;
        this.splitContainer.SplitterWidth = 1;
        this.splitContainer.TabIndex = 2;
        // 
        // button
        // 
        this.button.Location = new System.Drawing.Point(252, 7);
        this.button.Name = "button2";
        this.button.Size = new System.Drawing.Size(75, 23);
        this.button.TabIndex = 1;
        this.button.Text = "取消";
        this.button.UseVisualStyleBackColor = true;
        this.button.Click += new System.EventHandler(this.button2_Click);
        // 
        // button
        // 
        this.button.Location = new System.Drawing.Point(141, 8);
        this.button.Name = "button1";
        this.button.Size = new System.Drawing.Size(75, 23);
        this.button.TabIndex = 0;
        this.button.Text = "优化";
        this.button.UseVisualStyleBackColor = true;
        this.button.Click += new System.EventHandler(this.button1_Click);
        // 
        // Form
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(, 245);
        this.Controls.Add(this.splitContainer);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.Name = "Form";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "启动项管理器(请勾选开机时不自动运行的项目)";
        this.Load += new System.EventHandler(this.Form_Load);
        this.splitContainer.Panel1.ResumeLayout(false);
        this.splitContainer.Panel2.ResumeLayout(false);
        this.splitContainer.ResumeLayout(false);
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.ListView listView;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.ColumnHeader columnHeader;
    private System.Windows.Forms.SplitContainer splitContainer;
    private System.Windows.Forms.Button button;
    private System.Windows.Forms.Button button;
}