Mousa Alfhaily Mousa Alfhaily - 2 years ago 1104 Question

Auto-start my program in administrative mode when system starts up without the UAC prompt

My problem :

I made a program and i have this program for example now under this directory :

"C:\Program Files (x86)\AppName"
, now in the program i'm saving some images in my program directory, but since this is in the
drive, it's giving me a
access denied
error, now i made the program to always run in administrative mode and that worked just fine, but also there is in my program a feature to auto-start my program when the system starts up using a registry key, but when the "always run in administrative mode" is ON, the program will not start even though there is a start-up key in the registry, and when the "always run in administrative mode" is OFF, it will start.

What I have tried so far :

I tried making an another executable file that will start when the system starts up and then it will run my main program and close it self and i knew that will work but now every time i start my computer, the User Access Control prompt ask me if i trust this program even and that is not very user-friendly.

So i searched and i found that i can create scheduled task but i didn't find a lot of examples and but i tried creating one manually and it did work as wanted!

** But here is a code which didn't work :**

Imports Microsoft.Win32.TaskScheduler

Using ts As New TaskService("\\RemoteServer")
'Create a new task definition and assign properties
Dim td As TaskDefinition = ts.NewTask()
td.RegistrationInfo.Description = "Does something"

'Create a trigger that will fire the task at this time every other day
td.Triggers.Add(New DailyTrigger() With {
Key.DaysInterval = 2

'Create an action that will launch Notepad whenever the trigger fires
td.Actions.Add(New ExecAction("notepad.exe", "c:\test.log", Nothing))

'Register the task in the root folder
ts.RootFolder.RegisterTaskDefinition("Test", td)
End Using

And the problem with the code is :

But it couldn't find the
and it has no use of the

Hope someone have some previous experience with that who can help me!

Answer Source

To answer your question:

To use that code you've got to download the Managed Task Scheduler Wrapper first. Then to make it run with administrative privileges you've got to set the RunLevel to TaskRunLevel.Highest on your TaskDefinition:

td.Principal.RunLevel = TaskRunLevel.Highest

However like Plutonix says you shouldn't be writing files to the directory of your program (as that's usually located in the restricted %ProgramFiles% folder).

Instead, use the %ProgramData% directory. You can get the path to it from your code via Environment.GetFolderPath():


Dim ConfigPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "AppName")
Dim ImagePath As String = Path.Combine(ConfigPath, "MyImage.png")

If Directory.Exists(ConfigPath) = False Then Directory.CreateDirectory(ConfigPath)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download