简体   繁体   中英

Check if SCHTASKS file exists

I am making a backup program in c# in which I use a .bat script to create a new Tasks that runs my program on an interval. I need to make sure the .bat is only run if the task does not exist. To do this I use the following piece of code, but it doesn't seem to detect whether or not it exists.

if (!File.Exists(@"C:\Windows\System32\Tasks\BackupUtil.*"))
        {
         Process proc = new Process();
         proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
         proc.StartInfo.UseShellExecute = true;
         proc.StartInfo.FileName = @"C:\Users\Sebastian Esp\documents\visual studio 2012\Projects\FileBackUp_Sorter\FileBackUp_Sorter\Task_Schedule.bat";
         proc.Start();
         proc.WaitForExit();
        }

Use Directory.GetFiles instead

if (Directory.GetFiles(@"C:\Windows\System32\Tasks", "BackupUtil.*").Length == 0)
    //....Your code
}

http://msdn.microsoft.com/en-us/library/wz42302f(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM