欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#程序启动项的设置方法

程序员文章站 2023-12-04 16:47:46
本文为大家分享了c#程序启动项的设置方法,供大家参考,具体内容如下 托盘图标设置 新建一个notifyicon,会在托盘处显示一个图标。 notifyicon.i...

本文为大家分享了c#程序启动项的设置方法,供大家参考,具体内容如下

托盘图标设置

新建一个notifyicon,会在托盘处显示一个图标。

notifyicon.icon可以直接设置一个ico图片,也可以延用原有程序的图标。

notifyicon.icon = system.drawing.icon.extractassociatedicon(application.executablepath);

public partial class mainwindow : window
 {
  private notifyicon notifyicon;

  public mainwindow()
  {
   initializecomponent();
   setnotifyicon();
   this.hide();
  }

  #region notifyicon

  private void setnotifyicon()
  {
   this.notifyicon = new notifyicon();
   this.notifyicon.balloontiptext = "磁盘清理工具";
   this.notifyicon.showballoontip(2000);
   this.notifyicon.text = "磁盘清理工具:每20天清理一次";
   this.notifyicon.icon = system.drawing.icon.extractassociatedicon(application.executablepath);
   this.notifyicon.visible = true;
   //打开菜单项
   menuitem open = new menuitem("打开");
   open.click += new eventhandler(show);
   //退出菜单项
   menuitem exit = new menuitem("退出");
   exit.click += new eventhandler(close);
   //关联托盘控件
   menuitem[] childen = new menuitem[] { open, exit };
   notifyicon.contextmenu = new contextmenu(childen);

   this.notifyicon.mousedoubleclick += new mouseeventhandler((o, e) =>
   {
    if (e.button == mousebuttons.left) this.show(o, e);
   });
  }

  private void show(object sender, eventargs e)
  {
   this.visibility = visibility.visible;
   this.showintaskbar = true;
   this.activate();
  }

  private void hide(object sender, eventargs e)
  {
   this.showintaskbar = false;
   this.visibility = visibility.hidden;
  }

  private void close(object sender, eventargs e)
  {
   system.windows.application.current.shutdown();
  }

  #endregion

  #region 窗口

  private void minimizebutton_onclick(object sender, routedeventargs e)
  {
   windowstate = windowstate.minimized;
  }

  private void closebutton_onclick(object sender, routedeventargs e)
  {
   this.hide();
  }

  private void headergrid_onmouseleftbuttondown(object sender, mousebuttoneventargs e)
  {
   if (e.buttonstate == mousebuttonstate.pressed)
   {
    this.dragmove();
   }
  }

  #endregion
 }

禁用多进程启动

//禁止双进程
 bool cancreatenew;
 using (system.threading.mutex m = new system.threading.mutex(true, system.windows.forms.application.productname, out cancreatenew))
 {
  if (!cancreatenew)
  {
   this.shutdown();
  }
 }

删除原有进程

/// <summary>
 /// 删除原有进程
 /// </summary>
 /// <param name="processname"></param>
 private void killprocess(string processname)
 {
  //得到所有打开的进程 
  try
  {
   process currentprocess = process.getcurrentprocess();
   var processes = process.getprocessesbyname(processname).where(process=> process.id!=currentprocess.id);
   foreach (process thisproc in processes)
   {
    //找到程序进程,kill之。
    if (!thisproc.closemainwindow())
    {
     thisproc.kill();
    }
   }
  }
  catch (exception ex)
  {
    
  }
 }

设置开机自启动

private void setappautorun(bool autorun)
 {
  if (autorun) //设置开机自启动 
  {
   string path = system.windows.forms.application.executablepath;
   registrykey rk = registry.localmachine;
   registrykey rk2 = rk.createsubkey(@"software\microsoft\windows\currentversion\run");
   rk2.setvalue("jcshutdown", path);
   rk2.close();
   rk.close();
  }
  else //取消开机自启动 
  {
   registrykey rk = registry.localmachine;
   registrykey rk2 = rk.createsubkey(@"software\microsoft\windows\currentversion\run");
   rk2.deletevalue("jcshutdown", false);
   rk2.close();
   rk.close();
  }
 }

app.cs中完整代码:

public partial class app : application
 {
  public app()
  {
   //禁止双进程
   bool cancreatenew;
   using (system.threading.mutex m = new system.threading.mutex(true, system.windows.forms.application.productname, out cancreatenew))
   {
    if (!cancreatenew)
    {
     this.shutdown();
    }
   }

   setappautorun(true);

   startup += app_startup;
  }

  private void setappautorun(bool autorun)
  {
   if (autorun) //设置开机自启动 
   {
    messagebox.show("设置开机自启动,需要修改注册表", "提示"); // hovertree.com
    string path = system.windows.forms.application.executablepath;
    registrykey rk = registry.localmachine;
    registrykey rk2 = rk.createsubkey(@"software\microsoft\windows\currentversion\run");
    rk2.setvalue("jcshutdown", path);
    rk2.close();
    rk.close();
   }
   else //取消开机自启动 
   {
    messagebox.show("取消开机自启动,需要修改注册表", "提示");
    registrykey rk = registry.localmachine;
    registrykey rk2 = rk.createsubkey(@"software\microsoft\windows\currentversion\run");
    rk2.deletevalue("jcshutdown", false);
    rk2.close();
    rk.close();
   }
  }

  private void app_startup(object sender, startupeventargs e)
  {
   new autocleancachehelper(cleancacheveiwmodel.viewmodel).start();
  }
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。