如果我的程序不是在管理模式下运行,如何阻止它运行? XP 和 Windows 7

发布于 2024-12-28 15:11:19 字数 335 浏览 2 评论 0原文

我有一个 C# 程序,应该是多操作系统兼容的。它需要访问权限才能创建目录并获取 WMI 数据,但这仅在程序以管理员身份运行时可用。否则,就会失败。

如果程序没有检测到自己以管理员身份运行,是否可以使用任何命令来不运行该程序?我尝试添加 app.manifest 并使用“requireAdministrator”,它会提示登录,但这似乎仅适用于 Windows 7 和 Vista,不适用于 XP。

例子:

 if (isAdmin==0)
 Console.WriteLine("Please run this as an administrator");
 exit;

I have a C# Program that is supposed to be multi-OS compatible. It requires access to create a directory and get WMI Data, but that is only available if the program is ran as an Administrator. Otherwise, it fails.

Is there any command I can use to not run the program if it does not detect itself as being ran as an administrator? I tried adding a app.manifest and using "requireAdministrator", it prompts for login, but that appears to only work on Windows 7 and Vista, not XP.

Example:

 if (isAdmin==0)
 Console.WriteLine("Please run this as an administrator");
 exit;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

愁以何悠 2025-01-04 15:11:19

检查用户是否是管理员

public static bool IsAdministrator()
{
    WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
    WindowsPrincipal windowsPrincipal = new WindowsPrincipal(windowsIdentity);

    return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}

如果不是管理员,则重新启动应用程序

public static bool RestartAsAdministrator(string filePath, string fileName, string errorCaption)
{
    Process process = null;
    ProcessStartInfo processStartInfo = new ProcessStartInfo();
    processStartInfo.FileName = Path.Combine(filePath, fileName);

    if (Environment.OSVersion.Version.Major >= 5) //5 is XP and 6 is Vista and 7
        processStartInfo.Verb = "runas";

    processStartInfo.Arguments = "";
    processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
    processStartInfo.UseShellExecute = true;

    try
    {
        process = Process.Start(processStartInfo);
    }

    catch (Exception)
    {
        MessageBox.Show("Couldn't start as admin.\nPlease try manually by Right Clicking on " + Path.GetFileNameWithoutExtension(fileName) + " and selecting \"Run as administrator\"",
                            errorCaption + " Error", MessageBoxButton.OK, MessageBoxImage.Error);

        return false;
    }

    finally
    {
        if (process != null)
            process.Dispose();
    }

    return true;
}

应用程序清单文件(用于自动以管理员身份运行)

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

Check if the User Is an Admin

public static bool IsAdministrator()
{
    WindowsIdentity windowsIdentity = WindowsIdentity.GetCurrent();
    WindowsPrincipal windowsPrincipal = new WindowsPrincipal(windowsIdentity);

    return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}

Restart the app if not an admin

public static bool RestartAsAdministrator(string filePath, string fileName, string errorCaption)
{
    Process process = null;
    ProcessStartInfo processStartInfo = new ProcessStartInfo();
    processStartInfo.FileName = Path.Combine(filePath, fileName);

    if (Environment.OSVersion.Version.Major >= 5) //5 is XP and 6 is Vista and 7
        processStartInfo.Verb = "runas";

    processStartInfo.Arguments = "";
    processStartInfo.WindowStyle = ProcessWindowStyle.Normal;
    processStartInfo.UseShellExecute = true;

    try
    {
        process = Process.Start(processStartInfo);
    }

    catch (Exception)
    {
        MessageBox.Show("Couldn't start as admin.\nPlease try manually by Right Clicking on " + Path.GetFileNameWithoutExtension(fileName) + " and selecting \"Run as administrator\"",
                            errorCaption + " Error", MessageBoxButton.OK, MessageBoxImage.Error);

        return false;
    }

    finally
    {
        if (process != null)
            process.Dispose();
    }

    return true;
}

Application Manifest File (Used to automatically Run as Admin)

<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
微暖i 2025-01-04 15:11:19

您可以使用此功能检查当前用户(启动您的应用程序)是否具有管理员权限。

using System.Security.Principal;
...
bool IsAnAdministrator ()
{
   WindowsIdentity  identity = WindowsIdentity.GetCurrent();
   WindowsPrincipal principal = new WindowsPrincipal (identity);
   return principal.IsInRole(WindowsBuiltInRole.Administrator);
}

如果您调用此方法并且它返回false,您可以显示一条消息和/或关闭您的应用程序。

You can use this function to check if the current user (that starts your application) has administrator rights.

using System.Security.Principal;
...
bool IsAnAdministrator ()
{
   WindowsIdentity  identity = WindowsIdentity.GetCurrent();
   WindowsPrincipal principal = new WindowsPrincipal (identity);
   return principal.IsInRole(WindowsBuiltInRole.Administrator);
}

If you call this method and it returns false you can show a message and/or close your application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文