For Winforms (.NET), how do I create a method to check for Admin rights prior to the form loading?

Viewed 23

Working on creating a WINGET Application to provide a GUI for USMT. I want to check for Admin rights and if the program is in the right folder before the main form loads and close the application if that's not the case.

So, I create a class for this purpose so I don't have Spaghetti code in the Program class.

    public static class Setup
{ 
    public static bool IsAdministrator()
    {
            // Stuff
    }

    
    public static void CheckAdmin()
    {
        if (!IsAdministrator())
        {
                // Moar stuff
        }
    }
}

And then in the Program class, I've got this...

Setup.CheckAdmin();

But I get error IDE1007 - CheckAdmin does not exist in this context and I'm not sure what I'm missing.

1 Answers

I recreated this in a WinForms application and had no issues. Are you calling the CheckAdmin function in the Main function of the Program class?

I would think it would make more sense to do this setup in the Load event for your form rather than the Program class.

Related