问题
I can't for the life of me figure this one out. I have created a fairly large C# application that basically runs a bunch of separate components and reboots the machine periodically. The idea of this application is basically to automate a process that we have at my company. So I had it working previously so that after the machine reboots it launches after an account is logged into.
Anyways the app no longer will launch on startup. Please see the code below which I have implemented to add an entry to the Run key in the registry:
RegistryKey winStartUpKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run");
string shieldStartVal = (string)winStartUpKey.GetValue("SHIELDCustomizer");
if (shieldStartVal == null)
{
string currentName = System.AppDomain.CurrentDomain.FriendlyName;
string currentDIR = Directory.GetCurrentDirectory() + @"\" + currentName;
RegistryKey startUpKey = Registry.LocalMachine.OpenSubKey
(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", RegistryKeyPermissionCheck.ReadWriteSubTree);
startUpKey.SetValue("SHIELDCustomizer", "\"" + currentDIR + "\"", RegistryValueKind.String);
}
The above code has worked in the past. And I have verified that the startup entry is in-fact added to the registry. When I copy the path in the registry into a run prompt and click OK, the app starts without an issue.
I have made some small changes to the code (rather lengthy code, not sure where to begin posting this code) that I can't imagine having an impact on whether or not the app launches on startup. All of my images and files have been added as a resource to the project. The only reference I could really find that outlined this similar scenario is on this forum here: application won't start on startup after adding a notifyicon which actually doesn't look like it pertains to my current setup because my notifyIcon object and properties have been added using Visual Studio and I have not coded it manually.
Does anyone have any idea as to what might be causing a program to not launch at startup when the correct entry is in the registry? I know I haven't provided much code, please let me know if there is any additional information I can provide and I will be happy to do so.
Thank you so much in advance!
回答1:
Startup Tasks can be disabled using Task Manager in Windows 8.1 or msconfig in Windows 7. For example, I have disabled Box Sync from running at startup on my machine:
Enabling/Disabling though task manager sets a registry key at
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run
or
HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run
If the value starts with 02 00 00 then it is enabled. If it starts with another value (like 03 00 in my example) then it is disabled:
来源:https://stackoverflow.com/questions/29994315/c-sharp-application-not-running-on-startup