Skip to content

Commit 0bab2eb

Browse files
committed
update 3 days execution time limit removal to work on win11
1 parent 501bd62 commit 0bab2eb

1 file changed

Lines changed: 117 additions & 7 deletions

File tree

AppAutoStart.cs

Lines changed: 117 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ public static bool SetAutoStart(bool enable)
2121
{
2222
FileName = "schtasks.exe",
2323
CreateNoWindow = true,
24-
UseShellExecute = false
24+
UseShellExecute = false,
25+
RedirectStandardOutput = true,
26+
RedirectStandardError = true
2527
};
2628

2729
startInfo.Arguments = enable
@@ -32,30 +34,138 @@ public static bool SetAutoStart(bool enable)
3234
{
3335
using (Process process = Process.Start(startInfo))
3436
{
37+
string output = process.StandardOutput.ReadToEnd();
38+
string error = process.StandardError.ReadToEnd();
3539
process.WaitForExit();
40+
3641
if (process.ExitCode != 0)
3742
{
3843
Console.WriteLine($"Command exited with code {process.ExitCode}");
44+
Console.WriteLine($"Error: {error}");
3945
return false;
4046
}
4147
else if (enable)
4248
{
43-
// Remove the 3-day execution time limit using PowerShell
44-
var psInfo = new ProcessStartInfo
49+
// Create an XML file with the updated task settings to properly remove the time limit
50+
string tempXmlPath = Path.Combine(Path.GetTempPath(), $"{taskName}_task.xml");
51+
var psExport = new ProcessStartInfo
4552
{
4653
FileName = "powershell.exe",
47-
Arguments = $"-NoProfile -WindowStyle Hidden -Command \"Get-ScheduledTask -TaskName '{taskName}' | Set-ScheduledTask -Settings (New-ScheduledTaskSettingsSet -ExecutionTimeLimit 0)\"",
54+
Arguments = $"-NoProfile -Command \"Export-ScheduledTask -TaskName '{taskName}' | Out-File -FilePath '{tempXmlPath}'\"",
4855
CreateNoWindow = true,
4956
UseShellExecute = false
5057
};
51-
using (var psProcess = Process.Start(psInfo))
58+
59+
using (var psProcess = Process.Start(psExport))
5260
{
5361
psProcess.WaitForExit();
54-
if (psProcess.ExitCode != 0)
62+
}
63+
64+
if (File.Exists(tempXmlPath))
65+
{
66+
try
5567
{
56-
Console.WriteLine($"PowerShell command exited with code {psProcess.ExitCode}");
68+
var xmlDoc = new System.Xml.XmlDocument();
69+
xmlDoc.Load(tempXmlPath);
70+
71+
var nsManager = new System.Xml.XmlNamespaceManager(xmlDoc.NameTable);
72+
nsManager.AddNamespace("ns", "http://schemas.microsoft.com/windows/2004/02/mit/task");
73+
74+
// Find the Settings node
75+
var settingsNode = xmlDoc.SelectSingleNode("//ns:Settings", nsManager);
76+
if (settingsNode != null)
77+
{
78+
// Update or create ExecutionTimeLimit
79+
var executionTimeLimit = xmlDoc.SelectSingleNode("//ns:ExecutionTimeLimit", nsManager);
80+
if (executionTimeLimit != null)
81+
{
82+
executionTimeLimit.InnerText = "PT0S";
83+
}
84+
else
85+
{
86+
var newElement = xmlDoc.CreateElement("ExecutionTimeLimit", nsManager.LookupNamespace("ns"));
87+
newElement.InnerText = "PT0S";
88+
settingsNode.AppendChild(newElement);
89+
}
90+
91+
// Update IdleSettings
92+
var idleSettings = xmlDoc.SelectSingleNode("//ns:IdleSettings", nsManager);
93+
if (idleSettings != null)
94+
{
95+
// Remove Duration and WaitTimeout if they exist
96+
var duration = idleSettings.SelectSingleNode("ns:Duration", nsManager);
97+
if (duration != null)
98+
{
99+
idleSettings.RemoveChild(duration);
100+
}
101+
102+
var waitTimeout = idleSettings.SelectSingleNode("ns:WaitTimeout", nsManager);
103+
if (waitTimeout != null)
104+
{
105+
idleSettings.RemoveChild(waitTimeout);
106+
}
107+
}
108+
109+
// Update other settings
110+
var disallowStartIfOnBatteries = xmlDoc.SelectSingleNode("//ns:DisallowStartIfOnBatteries", nsManager);
111+
if (disallowStartIfOnBatteries != null)
112+
{
113+
disallowStartIfOnBatteries.InnerText = "false";
114+
}
115+
116+
var stopIfGoingOnBatteries = xmlDoc.SelectSingleNode("//ns:StopIfGoingOnBatteries", nsManager);
117+
if (stopIfGoingOnBatteries != null)
118+
{
119+
stopIfGoingOnBatteries.InnerText = "false";
120+
}
121+
122+
var allowHardTerminate = xmlDoc.SelectSingleNode("//ns:AllowHardTerminate", nsManager);
123+
if (allowHardTerminate != null)
124+
{
125+
allowHardTerminate.InnerText = "true";
126+
}
127+
}
128+
129+
xmlDoc.Save(tempXmlPath);
130+
131+
// Import the modified XML file
132+
var psImport = new ProcessStartInfo
133+
{
134+
FileName = "schtasks.exe",
135+
Arguments = $"/Create /F /TN \"{taskName}\" /XML \"{tempXmlPath}\"",
136+
CreateNoWindow = true,
137+
UseShellExecute = false,
138+
RedirectStandardOutput = true,
139+
RedirectStandardError = true
140+
};
141+
142+
using (var importProcess = Process.Start(psImport))
143+
{
144+
string importOutput = importProcess.StandardOutput.ReadToEnd();
145+
string importError = importProcess.StandardError.ReadToEnd();
146+
importProcess.WaitForExit();
147+
148+
if (importProcess.ExitCode != 0)
149+
{
150+
Console.WriteLine($"Task import error: {importError}");
151+
return false;
152+
}
153+
}
154+
}
155+
catch (Exception ex)
156+
{
157+
Console.WriteLine($"Error modifying task XML: {ex.Message}");
57158
return false;
58159
}
160+
finally
161+
{
162+
try { File.Delete(tempXmlPath); } catch { /* Ignore cleanup errors */ }
163+
}
164+
}
165+
else
166+
{
167+
Console.WriteLine("Failed to export task XML");
168+
return false;
59169
}
60170
}
61171
return true;

0 commit comments

Comments
 (0)