电脑启动时自动运行程序

时间:2014-10-04 08:44:26

标签: c#

我设计了一个应用程序,当他打开电脑时应该问候用户。该程序在我的电脑上运行良好,但当我在我兄弟的计算机上传输程序的.exe文件时,它不起作用意味着“当它启动时它不能自动工作”......我怎样才能克服这个问题?这是我的代码

注意:如果我手动执行程序,程序运行良好,但我希望它能自动运行。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;

namespace helloMSG
{
    public partial class Form1 : Form
{
    RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
    public Form1()
    {
        InitializeComponent();
        reg.SetValue("My app", Application.ExecutablePath.ToString());
    }

    private void Form1_Load(object sender, EventArgs e)
    {


    }
}

} `

2 个答案:

答案 0 :(得分:0)

目标计算机应符合要求,至少应安装正确的.net版本和其他依赖项(如果有)。由于您正在写入注册表,因此可能需要管理员权限。 还有一件事。你真的不需要一个winforms应用程序。控制台应该做的。

答案 1 :(得分:0)

如果您希望在Windows启动后启动程序,请将可执行文件复制到启动目录:

  1. 单击“开始”按钮“开始”按钮的图片,单击“所有程序”,右键单击“启动”文件夹,然后单击“打开”。
  2. 打开包含要为其创建快捷方式的项目的位置。
  3. 右键单击该项目,然后单击“创建快捷方式”。新快捷方式显示在与原始项目相同的位置。
  4. 将快捷方式拖到“启动”文件夹中。
  5. 以下是来源:http://windows.microsoft.com/en-gb/windows/run-program-automatically-windows-starts#1TC=windows-7

    如果程序未在您的朋友PC上运行,请确保.NET版本正确。事件查看器中可能有更多信息告诉您问题所在。