开发者

Visual Studio Console Application Installer - Schedule Windows Task

开发者 https://www.devze.com 2023-04-10 20:33 出处:网络
Does anyone know how to create an installation project using Visual Studio 2010 that creates a Windows Scheduler task?I\'m building an installer for a Console Application that needs to run every X min

Does anyone know how to create an installation project using Visual Studio 2010 that creates a Windows Scheduler task? I'm building an installer for a Console Application that needs to run every X minutes, and it would be nice for 开发者_StackOverflow社区the customer not to have to schedule it manually.

Thanks!


in Wix (.wixproj) you can do it in a CustomAction, written in Jscript, that invokes Schtasks.exe .

I don't know about VS2010's support of WIX, I think it is built-in.

The custom action module (the .js file) should have a function to run a schtasks command, something like this:

function RunSchtasksCmd(command, deleteOutput) {
    deleteOutput = deleteOutput || false;
    var shell = new ActiveXObject("WScript.Shell");
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    var tmpdir = fso.GetSpecialFolder(SpecialFolders.TemporaryFolder);
    var tmpFileName = fso.BuildPath(tmpdir, fso.GetTempName());
    var windir = fso.GetSpecialFolder(SpecialFolders.WindowsFolder);
    var schtasks = fso.BuildPath(windir,"system32\\schtasks.exe") + " " + command;

    // use cmd.exe to redirect the output
    var rc = shell.Run("%comspec% /c " + schtasks + "> " + tmpFileName, WindowStyle.Hidden, true);
    if (deleteOutput) {
        fso.DeleteFile(tmpFileName);
    }
    return {
        rc : rc,
        outputfile : (deleteOutput) ? null : tmpFileName
    };
}

And then, use that from within the custom action function itself, something like this

var r = RunSchtasksCmd("/Create Foo bar baz");
if (r.rc !== 0) {
    // 0x80004005 == E_FAIL
    throw new Error("exec schtasks.exe returned nonzero rc ("+r.rc+")");
}

var fso = new ActiveXObject("Scripting.FileSystemObject");
var textStream = fso.OpenTextFile(r.outputfile, OpenMode.ForReading);

// Read from the file and parse the results.
while (!textStream.AtEndOfStream) {
    var oneLine = textStream.ReadLine();
    var line = ParseOneLine(oneLine); // look for errors?  success?
}
textStream.Close();
fso.DeleteFile(r.outputfile);

Some people say writing CA's in script is the wrong thing to do, because they are hard to maintain, hard to debug, or it's hard to do them right. I think those are bad reasons. CA's implemented correctly in script, work well.


WIX has its own custom action for creating windows task and scheduling them.

<CustomAction Id="CreateScheduledTask" Return="check" Directory="Application" ExeCommand="&quot;[SystemFolder]SCHTASKS.EXE&quot; /CREATE /SC DAILY /TN &quot;My Task&quot; /ST 12:00:00 /TR &quot;[INSTALLLOCATION]Apps\File.exe&quot; /RU [%USERDOMAIN]\[LogonUser]" />

Above command will create a task with name 'My Task' which will execute everyday at 12 AM.

SCHTASKS command is used to create and schedule a windows task.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号