×

# windows计划任务自启动

hqy hqy 发表于2026-04-08 17:07:25 浏览7 评论0

抢沙发发表评论

# windows计划任务自启动

在windows下,命令调用schtasks.exe 创建与删除计划任务实现程序自启动

命令

C#
public sealed class Command{    public static string Windows(string arg, string[] commands)    {        return Execute("cmd.exe", arg, commands);    }    public static string Execute(string fileName, string arg, string[] commands)    {        Process proc = new Process();        proc.StartInfo.CreateNoWindow = true;        proc.StartInfo.FileName = fileName;        proc.StartInfo.UseShellExecute = false;        proc.StartInfo.RedirectStandardError = true;        proc.StartInfo.RedirectStandardInput = true;        proc.StartInfo.RedirectStandardOutput = true;        proc.StartInfo.Arguments = arg;        proc.StartInfo.Verb = "runas";        proc.Start();        if (commands.Length > 0)        {            for (int i = 0; i < commands.Length; i++)            {                proc.StandardInput.WriteLine(commands[i]);            }        }        proc.StandardInput.AutoFlush = true;        proc.StandardInput.WriteLine("exit");        string output = proc.StandardOutput.ReadToEnd();        proc.StandardError.ReadToEnd();        proc.WaitForExit();        proc.Close();        proc.Dispose();        return output;    }}

创建

C#
string str = "schtasks.exe /create /tn \"任务名\" /rl highest /sc ONSTART /delay 0000:30 /tr \"可执行程序绝对路径\" /f"Command.Windows("schtasks.exe", new string[] {str});

删除

C#
string str = "schtasks /delete  /TN \"任务名\" /f";Command.Windows("schtasks.exe", new string[] {str});


打赏

本文链接:https://www.kinber.cn/post/6426.html 转载需授权!

分享到:


推荐本站淘宝优惠价购买喜欢的宝贝:

image.png

 您阅读本篇文章共花了: 

群贤毕至

访客