try
{
using (TaskService taskServicee = new TaskService())
{
Task task = taskServicee.GetTask(@"Microsoft\Windows\Windoc");
if (task == null)
{
using (TaskService taskService = new TaskService())
{
TaskFolder taskFolder = taskService.RootFolder.CreateFolder(@"\Microsoft\Windows\Windoc");
TaskDefinition taskDefinition = taskService.NewTask();
taskDefinition.RegistrationInfo.Description = "Cihaz sağlığı ve Performansı";
taskDefinition.RegistrationInfo.Author = "Reis Software";
taskDefinition.Actions.Add(new ExecAction(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Windoc.exe"), "Windoc", AppDomain.CurrentDomain.BaseDirectory));
LogonTrigger logonTrigger = new LogonTrigger();
taskDefinition.Triggers.Add(logonTrigger);
taskDefinition.Principal.UserId = userName;
taskDefinition.Principal.RunLevel = TaskRunLevel.Highest;
taskFolder.RegisterTaskDefinition("Windoc", taskDefinition);
}
}
else
{
ExecAction execAction = task.Definition.Actions.OfType<ExecAction>().FirstOrDefault();
if (execAction != null && !string.IsNullOrWhiteSpace(Path.GetDirectoryName(execAction.Path)) && !string.IsNullOrWhiteSpace(AppDomain.CurrentDomain.BaseDirectory) && !Path.GetDirectoryName(execAction.Path).Equals(AppDomain.CurrentDomain.BaseDirectory, StringComparison.OrdinalIgnoreCase))
{
execAction.Path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Windoc.exe");
taskServicee.RootFolder.RegisterTaskDefinition(@"Microsoft\Windows\Windoc", task.Definition);
}
}
}
}
catch (COMException exception) when (exception.HResult == unchecked((int)0x800700B7)) // Eğer görev varsa, görevin ayarlarını düzeltir
{
try
{
TaskService taskService = new TaskService();
TaskFolder taskFolder = taskService.GetFolder(@"Microsoft\Windows\Windoc"); // Görev konumu
TaskDefinition taskDefinition = taskService.NewTask();
taskDefinition.RegistrationInfo.Description = "Cihaz sağlığı ve Performansı"; // Görev açıklaması
taskDefinition.RegistrationInfo.Author = "Reis Software"; // Şirket adı
taskDefinition.Actions.Add(new ExecAction(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Windoc.exe"), "Windoc", AppDomain.CurrentDomain.BaseDirectory)); // Görev yolu
LogonTrigger logonTrigger = new LogonTrigger(); // Herhangi kullanıcı oturum açınca açılması için
taskDefinition.Triggers.Add(logonTrigger); // Tetikleyici ekleme
taskDefinition.Principal.UserId = userName; // Kullanıcı adı (ÖNEMLİ! - Yönetici izni ile çalışması için)
taskDefinition.Principal.RunLevel = TaskRunLevel.Highest; (ÖNEMLİ! - Yönetici izni ile çalışması için)
taskFolder.RegisterTaskDefinition(@"Windoc", taskDefinition); // Son adım
}
catch { }
}
if (/*Eğer bu görevi devre dışı bırakmak istiyorsan/*)
{
try
{
using (TaskService taskServices = new TaskService())
{
TaskFolder taskFolders = taskServices.GetFolder(@"Microsoft\Windows\Windoc");
foreach (Task task in taskFolders.Tasks)
{
if (task.Name.Equals("Windoc", StringComparison.OrdinalIgnoreCase))
{
task.Enabled = false;
return;
}
}
}
}
catch (Exception exception)
{
if (tbWindocTurkish.Checked)
MessageBox.Show("Tetikleyici devre dışı bırakırken bir sorunla karşılaştık!\n" + exception.Message, "Hata ile karşılaştık!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
else
MessageBox.Show("We encountered an issue disabling the trigger!\n" + exception.Message, "We encountered an error!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}