Visual Studio .NET 2003WebForms.NET 3.0Visual Studio 2005Windows XP.NET 2.0C# 2.0中级开发Visual StudioWindows.NETVisual BasicASP.NETC#
如何按时执行程序(读写系统注册表值)
此代码将帮助您读取现有值或在系统注册表中创建值并按时执行程序。
下载 Sample.zip - 521.5 KB
引言
在创建程序时,我们中的一些人为了保存应用程序的设置,倾向于使用文件处理技术,例如 .ini / .dat 文件或 xml 文件,以便检查最近的设置。因此,我们可以通过简单地操作系统注册表文件来实现...
背景
这里的基本思想是如何在您的系统注册表中存储应用程序的设置。
- 读取和写入计算机系统注册表中的设置。
- 检查应用程序的最新状态。
- 将您的应用程序设置为 Windows 启动时运行。
使用代码
这是我使用 C# 语言(Dotnet Framework 版本 2.0 或更高版本)编写的示例代码。
此示例代码用于在 Windows 启动时执行应用程序,并每小时的 30 分钟执行一次。
<我实际上是为一些不正经的事情做的.. 嗯.. 让我说一下,通过意外地在他们的计算机上显示一张可怕的图片来吓唬某人> =} 尽情享受!!就在这里。
//
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
this.Hide();
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (DateTime.Now.Minute.ToString() == "30")
{
this.Show();
}
else
{
this.Hide();
}
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
timer1_Tick(sender, e);
bool isKeyExist = false;
string strSubkey = @"Software\Microsoft\Windows\CurrentVersion\Run";
RegistryKey Mykey = Registry.CurrentUser.OpenSubKey(strSubkey);
string[] strSearcKey = Mykey.GetValueNames();
Mykey.Close();
foreach (string strSearch in strSearcKey)
{
if (strSearch.ToUpper().Equals("ERIC"))
{
isKeyExist = true;
break;
}
}
if (isKeyExist == false)
{
Mykey = Registry.CurrentUser.CreateSubKey(strSubkey);
Mykey.SetValue("ERIC", Application.ExecutablePath, RegistryValueKind.String);
Mykey.Close();
}
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
}
}
}
//