-1

I have a WPF application and I want to offer a checkbox that configures if the app will start on Windows startup (or not).

For now, I have it working with this:

void ManageRunOnStartup(bool runOnStartup)
{
    var shortcutPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Startup), "myApp.lnk");
    var shortcutExists = System.IO.File.Exists(shortcutPath);

    if (runOnStartup)
    {
        if (shortcutExists == false)
            CreateShortcut(shortcutPath);
    }
    else if (shortcutExists)
    {
        System.IO.File.Delete(shortcutPath);
    }
}

void CreateShortcut(string shortcutPath)
{
    var appLocation = System.Reflection.Assembly.GetExecutingAssembly().Location;

    var shell = new WshShell();
    var shortcut = shell.CreateShortcut(shortcutPath) as IWshShortcut;
    shortcut.TargetPath = appLocation;
    shortcut.WorkingDirectory = Path.GetDirectoryName(appLocation);
    shortcut.Save();
}

The thing is, to write/delete a file in the Environment.SpecialFolder.Startup folder, the app requires Admin rights :(

Is there a way to achieve the same result but without requiring Admin rights?

CSDev
  • 3,177
  • 6
  • 19
  • 37
Bruno
  • 4,685
  • 7
  • 54
  • 105

1 Answers1

0

If a programm requires Admin rights or not, is first and foremost a mater of the Manifest. Wich is put into the Executeable, and thus not affectable.

If you got some other code starting the Application (like the Task Sheduler or the Service Manager) you can of course dictate a lower rights setting/specific user. If you got the rights for it.

It is also possible to store a value and check it - and if the programm is running elevated - during programm start. And then try a restart with Elevation. Application.Run has the Elevation request.

Getting rid of elevated rights in turn is something that is not easy at all.

Edit: Here some code on how to start any Programm elevated Programmatically.

using System;
using System.Diagnostics;
using System.IO;

namespace RunAsAdmin
{
    class Program
    {
        static void Main(string[] args)
        {
            /*Note: Running a batch file (.bat) or similar script file as admin
            Requires starting the interpreter as admin and handing it the file as Parameter 
            See documentation of Interpreting Programm for details */

            //Just getting the Absolute Path for Notepad
            string windir = Environment.GetFolderPath(Environment.SpecialFolder.Windows);
            string FullPath = Path.Combine(windir, @"system32\notepad.exe");

            //The real work part
            ProcessStartInfo startInfo = new ProcessStartInfo(FullPath);
            startInfo.Verb = "runas";
            System.Diagnostics.Process.Start(startInfo);
        }
    }
}
Christopher
  • 9,634
  • 2
  • 17
  • 31
  • I created a manifest - and now when I run my app I get the Admin rights popup - just for this feature yes...this is what I would like to avoid, ideally – Bruno Jul 23 '19 at 19:45
  • @ibiza: 1) Change the manifest so it does not request elevation. 2. Have a check for the function that actually needs elevation, wich will just (try to) restart the application elevated. You can find such checks on anything from TreeSize to the Windows Disk Cleaning Tool. Here is an example: https://www.codeproject.com/Articles/105506/Getting-Elevated-Privileges-on-Demand-using-C – Christopher Jul 23 '19 at 19:50
  • @ibiza I added exampel code for (trying to) start any programm as Administrator. One issue might be getting the path of the programm, but I think the (t)rusty Commandline Parameters should contain the Path and filename under wich your programm was started. Asuming nobody meesed up the "run in" Directory, even a relative path should do. – Christopher Jul 23 '19 at 19:58
  • Thanks! I might end up resorting to doing this...I was looking for other methods than having a shortcut in the Startup folder to run a program on Windows starting – Bruno Jul 23 '19 at 22:15
  • @ibiza If you want to run a programm on windows start, that is a whole nother thematic entirely unrelated to it running elevated. While you got the services, I would advise against it. Too many limits. The Task Sheduler is way better and even Microsoft itself started using a lot of shedulable processes from services over to the sheduler: https://stackoverflow.com/questions/7394806/creating-scheduled-tasks Just do not mix it up with the C# class of the same name - afaik this one is about Multithreading, a whole other beast. – Christopher Jul 23 '19 at 23:50