c#开机运行遇到的问题
/// <summary>
/// 开机运行
/// </summary>
private static void SetAutoRun()
{
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);
}
用上面的方法使程序开机运行后下面红色部分出错
string processName = "CJ";
private void timer1_Tick(object sender, EventArgs e)
{
Process[] processes = Process.GetProcessesByName(processName);
if (processes.Length == 0)
{
try
{
time = 0;
ProcessStartInfo procInfo = new ProcessStartInfo();
procInfo.FileName = processName;
procInfo.WindowStyle = ProcessWindowStyle.Hidden;
Process proc = Process.Start(procInfo);
}
catch (Exception ex)
{
}}
不是开机运行的时候没错