[英]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?您是在调用
CheckAdmin
的Main
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.