C# winfrom 开机启动项设置
lvStartItems为listview控件
#region 获取注册表名和值 private void GetStartItemsName() { RegistryKey regKey = Registry.LocalMachine; using (RegistryKey startKey = regKey.OpenSubKey(startString, true)) { string[] arrStartItems = startKey.GetValueNames(); foreach (string startName in arrStartItems) { ListViewItem lvi = new ListViewItem(); lvi.SubItems[0].Text = startName; //名称 lvi.SubItems.Add(startKey.GetValue(startName).ToString()); //位置 lvStartItems.Items.Add(lvi); } } } #endregion #region 设置开机启动项 private bool RunWhenStart(bool started, string startName, string startPath) { RegistryKey regKey = Registry.LocalMachine; using (RegistryKey runKey = regKey.OpenSubKey(startString, true)) { if (started) //添加 { try { runKey.SetValue(startName, startPath); return true; } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } else //删除 { try { runKey.DeleteValue(startName); return true; } catch (Exception ex) { MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } } } #endregion
#region 添加启动项 private void tsmiAdd_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); if (ofd.ShowDialog() == DialogResult.OK) { string fileName = ofd.SafeFileName.Remove(ofd.SafeFileName.IndexOf(".")); string filePath = ofd.FileName; if (RunWhenStart(true, fileName, filePath)) { ListViewItem lvi = new ListViewItem(); lvi.SubItems[0].Text = fileName; lvi.SubItems.Add(filePath); lvStartItems.Items.Add(lvi); MessageBox.Show("添加启动项成功 ^_^ ", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } #endregion #region 删除启动项 private void tsmiDel_Click(object sender, EventArgs e) { if (lvStartItems.SelectedItems.Count > 0) { int removeIndex = -1; removeIndex = lvStartItems.SelectedItems[0].Index; string removeName = lvStartItems.SelectedItems[0].Text.ToString(); string removePath = lvStartItems.Items[removeIndex].SubItems[1].Text; if (removeIndex > -1) { if (RunWhenStart(false, removeName, removePath)) { lvStartItems.Items.RemoveAt(removeIndex); MessageBox.Show("【" + removeName + "】启动项删除成功 ^_^ ", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } else { MessageBox.Show("请选中要移除的项!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } #endregion
增加拖放功能
private void lvStartItems_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.Copy; } else { e.Effect = DragDropEffects.None; } } private void lvStartItems_DragDrop(object sender, DragEventArgs e) { String[] files = e.Data.GetData(DataFormats.FileDrop, false) as String[]; foreach (string fileStr in files) { ListViewItem lvi = new ListViewItem(); string keyName = fileStr.Substring(fileStr.LastIndexOf("\\") + 1, fileStr.LastIndexOf(".") - fileStr.LastIndexOf("\\") - 1); if (RunWhenStart(true, keyName, fileStr)) { lvi.SubItems[0].Text = keyName; lvi.SubItems.Add(fileStr); lvStartItems.Items.Add(lvi); MessageBox.Show("添加启动项成功 ^_^ ", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }