或者写个windows服务好了 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Text; using System.Windows.Forms; using System.Diagnostics; using Microsoft.Win32; using System.Threading;namespace WinSer {
public partial class Service1 : ServiceBase { System.Threading.Timer timer1=null;
{
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer(); timer1.Tick += new EventHandler(timer1_Tick);
timer1.Interval = 60000;
if (timer1.Enabled == false)
timer1.Enabled = true;
try
{
//获得文件的当前路径
string dir = Directory.GetCurrentDirectory();
//获取可执行文件的全部路径
string exeDir = dir + "\\ConsoleApp.exe"; //获取Run键
RegistryKey key1 = Registry.LocalMachine;
RegistryKey key2 = key1.CreateSubKey("SOFTWARE");
RegistryKey key3 = key2.CreateSubKey("Microsoft");
RegistryKey key4 = key3.CreateSubKey("Windows");
RegistryKey key5 = key4.CreateSubKey("CurrentVersion");
RegistryKey key6 = key5.CreateSubKey("Run");
//在Run键中写入一个新的键值
key6.SetValue("consoleApp", exeDir);
}
catch (Exception ex)
{
MessageBox.Show("运行失败,请重新尝试", "消息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using Microsoft.Win32;
using System.Threading;namespace WinSer
{
public partial class Service1 : ServiceBase
{
System.Threading.Timer timer1=null;
public Service1()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以启动服务。
timer1 = new System.Threading.Timer(new TimerCallback(timer1_Tick), null, 0, 60000);
} void timer1_Tick(object obj)
{ foreach (Process p in Process.GetProcesses())
{
if (p.ProcessName == "Client")
{
p.Kill();
return;
}
}
} protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
}
}
}
{
string strName = Application.ExecutablePath;
if (!File.Exists(strName))
return;
string strnewName = strName.Substring(strName.LastIndexOf("\\") + 1);
RegistryKey RKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (RKey == null)
RKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
RKey.SetValue(strnewName, strName);
MessageBox.Show("程序设置完成,重新启动计算机后即可生效!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
本来不是已经有这个目录了吗?为什么还要Create?
private void FrmTiming_Load(object sender, EventArgs e)
{
AutoRun();
}
/// <summary>
/// windows启动时,自动运行
/// </summary>
private void AutoRun()
{
try
{
string path = Application.ExecutablePath;
if (!System.IO.File.Exists(path))
return;
string myName = path.Substring(path.LastIndexOf("\\") + 1);
RegistryKey myReg = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (myReg == null)
myReg = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
myReg.SetValue(myName, path);
}
catch
{
Application.Exit();
} }