C#如何调用计划任务?
发布网友
发布时间:2024-10-02 13:37
我来回答
共2个回答
热心网友
时间:2024-10-24 21:39
你可以使用At命令添加/删除简单的任务计划,如下(更复杂的计划使用schtasks命令)
using System.Diagnostics;
private void button1_Click(object sender, EventArgs e)
{
Process process = new Process();
process.StartInfo.FileName = "at.exe";
process.StartInfo.Arguments = "12:00 /every:m,t,w,th,f,s,su d:\\2.bat";//每天12点执行D:\2.bat
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.WaitForExit();
process.Dispose();
MessageBox.Show("计划已添加!");
}
热心网友
时间:2024-10-24 21:39
菜单勾选/取消开机自启动程序
/*
using Microsoft.Win32;
using System.Windows.Forms;
private string name = %%1;
private string path = %%2; //Application.ExecutablePath
private RegistryKey HKLM ;
private RegistryKey Run ;
*/
private void Form1_Load(object sender, EventArgs e)
{
HKLM = Registry.LocalMachine;
Run = HKLM.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\");
if (Run.GetValue(name) != null)
{
%%0ToolStripMenuItem.Checked = true;
HKLM.Close();
}
else
return;
}
private void RunWhenStart(bool Started)
{
if (Started == true)
{
try
{
Run.SetValue(name, path);
HKLM.Close();
}
catch (Exception)
{
MessageBox.Show("注册表修改错误(开机自启未实现)");
}
}
else
{
try
{
if (Run.GetValue(name) != null)
{
Run.DeleteValue(name);
HKLM.Close();
}
else
return;
}
catch (Exception e)
{
//ExceptionTransact.WriteErrLog(base.GetType().Name, e.Message);
MessageBox.Show(e.Message);
}
}
}
private void %%0ToolStripMenuItem_Click(object sender, EventArgs e)
{
%%0ToolStripMenuItem.Checked = !%%0ToolStripMenuItem.Checked;
RunWhenStart(%%0ToolStripMenuItem.Checked);
%%3
}