简体   繁体   English

对于 Winforms (.NET),如何在表单加载之前创建一种方法来检查管理员权限?

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

Working on creating a WINGET Application to provide a GUI for USMT.致力于创建 WINGET 应用程序以为 USMT 提供 GUI。 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.因此,我为此目的创建了一个 class,因此程序 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...然后在程序 class 中,我有这个......

Setup.CheckAdmin();

But I get error IDE1007 - CheckAdmin does not exist in this context and I'm not sure what I'm missing.但我收到错误 IDE1007 - CheckAdmin 在此上下文中不存在,我不确定我缺少什么。

I recreated this in a WinForms application and had no issues.我在 WinForms 应用程序中重新创建了它,没有任何问题。 Are you calling the CheckAdmin function in the Main function of the Program class?您是在调用CheckAdminMain function 中的Program function 吗?

I would think it would make more sense to do this setup in the Load event for your form rather than the Program class.我认为在表单的Load事件中进行此设置而不是Program class 更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c#如何在加载表单之前阻止“ CheckBox CheckedChanged”启动? - c# How do I stop “CheckBox CheckedChanged” from firing prior to form loading? 如何授予我的 Windows 服务管理员权限 - How do I give my windows service admin rights 我如何显示“正在加载 . . . 请稍候”Winforms 中的消息以获取长加载表单? - How do I show a “Loading . . . please wait” message in Winforms for a long loading form? 如何创建一个 windows 应用程序,该应用程序应该在.Net C# 中以管理员权限运行,而不会一次又一次地提示 UAC? - How can I create a windows application that should run with admin rights in .Net C# without prompting UAC again and again? 如何检查管理员权限 C# - How to check admin rights C# 单个方法的管理员权限 - Admin rights for a single method 如何在WinForms应用程序中放置表单? - How do I dispose a form in a WinForms application? Winforms:如何显示“加载”表单? - Winforms: How to display a “loading” form? 安装没有管理员权限的 WinForms 应用程序 - Install WinForms application without admin rights 如何在没有管理员权限的情况下安装我的C#应用​​程序? - How Do I Make My C# Application Install Without Admin Rights?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM