简体   繁体   English

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

[英]How can I prevent my program from running if it is not ran in Administrative mode? XP and Windows 7

I have a C# Program that is supposed to be multi-OS compatible. 我有一个C#程序,应该是多操作系统兼容的。 It requires access to create a directory and get WMI Data, but that is only available if the program is ran as an Administrator. 它需要访问权限才能创建目录并获取WMI数据,但只有在以管理员身份运行程序时才可以使用。 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. 我尝试添加app.manifest并使用“requireAdministrator”,它会提示登录,但这似乎只适用于Windows 7和Vista,而不是XP。

Example: 例:

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

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" />

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. 如果您调用此方法并返回false ,则可以显示消息和/或关闭应用程序。

暂无
暂无

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

相关问题 我作为Windows服务运行的C#程序阻止Windows XP进入休眠状态 - My C# program running as Windows Service is blocking Windows XP from hibernation 以管理模式运行的程序是否需要以非管理模式运行另一个应用程序? - Program running in administrative mode needs to run another application in non-administrative mode? 如何在文件删除Windows XP上获得管理特权 - how to get administrative privilege on file deletion windows xp 在UWP应用(Windows 10)中,如何防止Windows操作系统进入睡眠模式? - In UWP app (Windows 10), how can I prevent Windows OS from entering Sleep mode? 如何在隐藏模式下从我的 C# 程序运行程序? - How can I run a program from my C# program in hidden mode? 如何在登录Windows XP之前运行我的程序? - How to run my program on before login on windows XP? 如何在Windows Server 2003和Windows XP中延迟创建的服务? - How can I delay my created service in Windows Server 2003 & Windows XP? 如何在C#中知道程序在运行XP SP2的Mac上的Parallels上运行,还是在运行Windows 7的Mac上的VmWare上运行 - How to know in C# that program is running on Parallels on a Mac running XP SP2 or VmWare on a Mac running Windows 7 如何防止Windows显示屏幕保护程序或从应用程序关闭电源? - How can i prevent my windows from showing screensaver or powering down from my application? 如何在内存中运行时防止我的程序集被保存? - How Can I Prevent My Assembly From Being Saved While Running in Memory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM