Skip to content

Commit d480e40

Browse files
committed
Fixed run on startup - thanks to Shelby
1 parent 1eee44d commit d480e40

11 files changed

+52
-121
lines changed

Advanced Installer/Advanced Installer.aip

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<COMPONENT cid="caphyon.advinst.msicomp.MsiPropsComponent">
77
<ROW Property="AI_BITMAP_DISPLAY_MODE" Value="0"/>
88
<ROW Property="AI_FINDEXE_TITLE" Value="Select the installation package for [|ProductName]" ValueLocId="AI.Property.FindExeTitle"/>
9-
<ROW Property="AI_PROPPATH_DIR_PERBUILD_AssistantComputerControl.exe" Value="..\AssistantComputerControl\bin\Release"/>
10-
<ROW Property="AI_PROPPATH_DIR_PERBUILD_AssistantComputerControl.exe.config" Value="..\AssistantComputerControl\bin\Release"/>
9+
<ROW Property="AI_PROPPATH_DIR_PERBUILD_AssistantComputerControl.exe" Value="..\AssistantComputerControl\bin\Debug"/>
10+
<ROW Property="AI_PROPPATH_DIR_PERBUILD_AssistantComputerControl.exe.config" Value="..\AssistantComputerControl\bin\Debug"/>
1111
<ROW Property="AI_PROPPATH_DIR_PERBUILD_Microsoft.Win32.TaskScheduler.dll" Value="..\packages\TaskScheduler.2.8.20\lib\net452"/>
1212
<ROW Property="AI_PROPPATH_DIR_PERBUILD_Microsoft.WindowsAPICodePack.Shell.dll" Value="..\packages\Microsoft.WindowsAPICodePack.Shell.1.1.0\lib"/>
1313
<ROW Property="AI_PROPPATH_DIR_PERBUILD_Microsoft.WindowsAPICodePack.dll" Value="..\packages\Microsoft.WindowsAPICodePack.Core.1.1.0\lib"/>
@@ -42,10 +42,10 @@
4242
<ROW Property="BannerBitmap" Value="banner" MultiBuildValue="DefaultBuild:banner.png" Type="1" MsiKey="BannerBitmap"/>
4343
<ROW Property="DialogBitmap" Value="dialog" MultiBuildValue="DefaultBuild:dialogbitmap.png" Type="1" MsiKey="DialogBitmap"/>
4444
<ROW Property="Manufacturer" Value="Albert MN."/>
45-
<ROW Property="ProductCode" Value="1033:{1AE13B1D-FE8D-4745-98E8-98C9B5DFCF38} " Type="16"/>
45+
<ROW Property="ProductCode" Value="1033:{0371981B-D00D-4213-ACE4-C5D20778B231} " Type="16"/>
4646
<ROW Property="ProductLanguage" Value="1033"/>
4747
<ROW Property="ProductName" Value="AssistantComputerControl"/>
48-
<ROW Property="ProductVersion" Value="1.4.2" Type="32"/>
48+
<ROW Property="ProductVersion" Value="1.4.3" Type="32"/>
4949
<ROW Property="SecureCustomProperties" Value="OLDPRODUCTS;AI_NEWERPRODUCTFOUND;AI_SETUPEXEPATH;SETUPEXEDIR"/>
5050
<ROW Property="UpgradeCode" Value="{8E6F4399-B513-420B-8E11-36837A8550EE}"/>
5151
<ROW Property="WindowsType9X" MultiBuildValue="DefaultBuild:Windows 9x/ME" ValueLocId="-"/>

AssistantComputerControl/AssistantComputerControl.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
77
<ProjectGuid>{404B42F4-E135-4D2F-8FD0-20A590814930}</ProjectGuid>
8-
<OutputType>WinExe</OutputType>
8+
<OutputType>Exe</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>AssistantComputerControl</RootNamespace>
1111
<AssemblyName>AssistantComputerControl</AssemblyName>
@@ -35,7 +35,7 @@
3535
<CreateWebPageOnPublish>true</CreateWebPageOnPublish>
3636
<WebPage>publish.htm</WebPage>
3737
<ApplicationRevision>0</ApplicationRevision>
38-
<ApplicationVersion>1.4.2.%2a</ApplicationVersion>
38+
<ApplicationVersion>1.4.3.%2a</ApplicationVersion>
3939
<UseApplicationTrust>false</UseApplicationTrust>
4040
<CreateDesktopShortcut>true</CreateDesktopShortcut>
4141
<PublishWizardCompleted>true</PublishWizardCompleted>

AssistantComputerControl/MainProgram.cs

Lines changed: 43 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
/*
22
* AssistantComputerControl
33
* Made by Albert MN.
4-
* Updated: v1.4.2, 12-12-2020
5-
* Updated: v1.4.2, 18-04-2020
4+
* Updated: v1.4.3, 20-04-2021
65
*
76
* Use:
87
* - Main class. Starts everything.
@@ -27,8 +26,8 @@
2726

2827
namespace AssistantComputerControl {
2928
class MainProgram {
30-
public const string softwareVersion = "1.4.2",
31-
releaseDate = "2020-12-12 14:55:00", //YYYY-MM-DD H:i:s - otherwise it gives an error
29+
public const string softwareVersion = "1.4.3",
30+
releaseDate = "2021-04-20 23:18:00", //YYYY-MM-DD H:i:s - otherwise it gives an error
3231
appName = "AssistantComputerControl",
3332

3433
sentryToken = "super_secret";
@@ -73,6 +72,8 @@ static void Main(string[] args) {
7372
Console.WriteLine("Log location; " + logFilePath);
7473
CheckSettings();
7574

75+
76+
7677
var config = new NLog.Config.LoggingConfiguration();
7778
var logfile = new NLog.Targets.FileTarget("logfile") { FileName = logFilePath };
7879
var logconsole = new NLog.Targets.ConsoleTarget("logconsole");
@@ -278,6 +279,8 @@ void ActualMain() {
278279

279280
RegistryKey key = Registry.CurrentUser.OpenSubKey("Software", true);
280281
if (Registry.GetValue(key.Name + @"\AssistantComputerControl", "FirstTime", null) == null) {
282+
SetStartup(true);
283+
281284
key.CreateSubKey("AssistantComputerControl");
282285
key = key.OpenSubKey("AssistantComputerControl", true);
283286
key.SetValue("FirstTime", false);
@@ -399,66 +402,6 @@ static void SystemEvents_SessionSwitch(object sender, SessionSwitchEventArgs e)
399402
}
400403
}
401404

402-
private static bool UpdateUserTaskInScheduler(string action)
403-
{
404-
try
405-
{
406-
ProcessStartInfo startInfo = new ProcessStartInfo();
407-
startInfo.FileName = "cmd.exe";
408-
startInfo.Arguments = "/C schtasks /query /TN \"AssistantComputerControl startup\""; //Check if task exists
409-
startInfo.RedirectStandardOutput = true;
410-
startInfo.UseShellExecute = false;
411-
startInfo.CreateNoWindow = true;
412-
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
413-
if (System.Environment.OSVersion.Version.Major < 6)
414-
{
415-
startInfo.Verb = "runas";
416-
}
417-
using (Process process = Process.Start(startInfo))
418-
{
419-
// Read in all the text from the process with the StreamReader.
420-
using (StreamReader reader = process.StandardOutput)
421-
{
422-
string stdout = reader.ReadToEnd();
423-
if (stdout.Contains("<<TaskName>>")) //If task exists
424-
{
425-
startInfo.RedirectStandardOutput = false;
426-
startInfo.UseShellExecute = true;
427-
switch (action)
428-
{
429-
case "Enable":
430-
startInfo.Arguments = "/C schtasks /Change /TN \"AssistantComputerControl startup\" /Enable";
431-
break;
432-
433-
case "Disable":
434-
startInfo.Arguments = "/C schtasks /Change /TN \"AssistantComputerControl startup\" /Disable";
435-
break;
436-
437-
case "Run":
438-
startInfo.Arguments = "/C schtasks /RUN /TN \"AssistantComputerControl startup\"";
439-
break;
440-
}
441-
Process.Start(startInfo).WaitForExit();
442-
}
443-
else
444-
{
445-
return false;
446-
}
447-
stdout = null;
448-
reader.Close();
449-
reader.Dispose();
450-
}
451-
}
452-
startInfo = null;
453-
return true;
454-
}
455-
catch (Exception ex)
456-
{
457-
MessageBox.Show(ex.Message);
458-
return false;
459-
}
460-
}
461-
462405
public static void TaskSchedulerSetup () {
463406
//Create "Task Scheduler" service; cleanup ACC on startup, log on, workstation unlock
464407
try {
@@ -474,39 +417,12 @@ public static void TaskSchedulerSetup () {
474417
td.Triggers.Add(new SessionStateChangeTrigger { StateChange = TaskSessionStateChangeType.SessionUnlock });
475418
td.Actions.Add(new ExecAction("powershell.exe", $"-WindowStyle Hidden -file \"{ps1File}\" \"{Path.Combine(MainProgram.CheckPath(), "*")}\" \"*.{Properties.Settings.Default.ActionFileExtension}\"", null));
476419

477-
// Register the task in the root folder
420+
//Register the task in the root folder
478421
ts.RootFolder.RegisterTaskDefinition(@"AssistantComputerControl cleanup", td);
479-
480-
// Remove the task we just created
481-
//ts.RootFolder.DeleteTask("Test");
482422
}
483423
} catch {
484424
DoDebug("Failed to create / update Task Scheduler service");
485425
}
486-
//Create "Task Scheduler" service; run ACC on startup & log on, added by Shelby Marvell
487-
try
488-
{
489-
using (TaskService ts = new TaskService())
490-
{
491-
var ps1File = Path.Combine(MainProgram.currentLocation, "ExtraCleanupper.ps1");
492-
493-
TaskDefinition td = ts.NewTask();
494-
td.Principal.LogonType = TaskLogonType.S4U;
495-
td.Principal.RunLevel = TaskRunLevel.Highest;
496-
td.RegistrationInfo.Author = "Albert MN. | AssistantComputerControl";
497-
td.RegistrationInfo.Description = "AssistantComputerControl startup - Runs ACC on reboot/login";
498-
td.Triggers.Add(new BootTrigger());
499-
td.Triggers.Add(new LogonTrigger());
500-
td.Actions.Add(new ExecAction(Application.ExecutablePath, null, null));
501-
502-
// Register the task in the root folder
503-
ts.RootFolder.RegisterTaskDefinition(@"AssistantComputerControl startup", td);
504-
}
505-
}
506-
catch
507-
{
508-
DoDebug("Failed to create / update Task Scheduler startup service");
509-
}
510426
}
511427

512428
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs args) {
@@ -711,21 +627,39 @@ public static void Exit() {
711627

712628
public static void SetStartup(bool status, bool setThroughSoftware = false) {
713629
try {
714-
bool res = false;
715630
if (status) {
716-
res = UpdateUserTaskInScheduler("Disable");
631+
//Create "Task Scheduler" service; run ACC on startup & log on, added by Shelby Marvell
632+
try {
633+
using (TaskService ts = new TaskService()) {
634+
var ps1File = Path.Combine(MainProgram.currentLocation, "ExtraCleanupper.ps1");
635+
636+
TaskDefinition td = ts.NewTask();
637+
td.Principal.LogonType = TaskLogonType.S4U;
638+
td.Principal.RunLevel = TaskRunLevel.Highest;
639+
td.RegistrationInfo.Author = "Albert MN. | AssistantComputerControl";
640+
td.RegistrationInfo.Description = "AssistantComputerControl startup - Runs ACC on reboot/login";
641+
td.Triggers.Add(new BootTrigger());
642+
td.Triggers.Add(new LogonTrigger());
643+
td.Actions.Add(new ExecAction(Application.ExecutablePath, null, null));
644+
645+
//Register the task in the root folder
646+
ts.RootFolder.RegisterTaskDefinition(@"AssistantComputerControl startup", td);
647+
}
648+
} catch {
649+
DoDebug("Failed to create / update Task Scheduler startup service");
650+
}
717651
} else {
718-
res = UpdateUserTaskInScheduler("Enable");
719-
}
720-
while (!res) {
721-
// Some error occurred. Try recreating the task.
722-
TaskSchedulerSetup();
723-
if (status) {
724-
res = UpdateUserTaskInScheduler("Disable");
725-
} else {
726-
res = UpdateUserTaskInScheduler("Enable");
652+
//Create "Task Scheduler" service; run ACC on startup & log on, added by Shelby Marvell
653+
try {
654+
using (TaskService ts = new TaskService()) {
655+
// Register the task in the root folder
656+
ts.RootFolder.DeleteTask(@"AssistantComputerControl startup");
657+
}
658+
} catch {
659+
DoDebug("Failed to create / update Task Scheduler startup service");
727660
}
728661
}
662+
729663
} catch {
730664
DoDebug("Failed to start ACC with Windows");
731665
if (!setThroughSoftware) {
@@ -736,18 +670,14 @@ public static void SetStartup(bool status, bool setThroughSoftware = false) {
736670

737671
public static bool ACCStartsWithWindows() {
738672
try {
739-
RegistryKey rk = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
740-
741-
var theVal = rk.GetValue(appName);
742-
if (theVal != null) {
743-
return true;
744-
} else {
745-
return false;
673+
using (TaskService ts = new TaskService()) {
674+
return ts.GetTask(@"AssistantComputerControl startup") != null;
746675
}
747-
} catch {
748-
DoDebug("Failed to get ACC start with windows state");
749-
return false;
676+
} catch (Exception e) {
677+
DoDebug("Something went wrong with TaskService, checking if ACC starts with Windows; " + e.Message);
750678
}
679+
680+
return false;
751681
}
752682

753683
public static bool HasInternet() {

AssistantComputerControl/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@
3333
// You can specify all the values or you can default the Build and Revision Numbers
3434
// by using the '*' as shown below:
3535
// [assembly: AssemblyVersion("1.0.*")]
36-
[assembly: AssemblyVersion("1.4.2.0")]
37-
[assembly: AssemblyFileVersion("1.4.2.0")]
36+
[assembly: AssemblyVersion("1.4.3.0")]
37+
[assembly: AssemblyFileVersion("1.4.3.0")]
3838
[assembly: NeutralResourcesLanguage("en")]
0 Bytes
Binary file not shown.

AssistantComputerControl/obj/Debug/AssistantComputerControl.csproj.FileListAbsolute.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,4 @@ C:\Users\alber\Dropbox\Programmering\C#\AssistantComputerControl\AssistantComput
162162
C:\Users\alber\Dropbox\Programmering\C#\AssistantComputerControl\AssistantComputerControl\bin\Debug\Translations\Italian.json
163163
C:\Users\alber\Dropbox\Programmering\C#\AssistantComputerControl\AssistantComputerControl\bin\Debug\Translations\Turkce.json
164164
C:\Users\alber\Dropbox\Programmering\C#\AssistantComputerControl\AssistantComputerControl\bin\Debug\Translations\Portuguese - Brasil.json
165+
C:\Users\alber\Dropbox\Programmering\C#\AssistantComputerControl\AssistantComputerControl\bin\Debug\Translations\Dutch.json
-512 Bytes
Binary file not shown.
-512 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)