WPF开发之以管理员身份运行2

72 篇文章 9 订阅

wpf 以管理员运行1
代码形式

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Reflection;
using System.Security.Principal;
using System.Threading;
using System.Windows;
using SchoolClient.Utils;
using SchoolClient.Wins;

namespace SchoolClient
{
    public partial class MyApp : Application
    {
        [STAThread]
        private static void Main()
        {
            new MyApp();
        }

        public MyApp()
        {
            /**
            * 当前用户是管理员的时候,直接启动应用程序
            * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
            */
            //获得当前登录的Windows用户标示
            WindowsIdentity identity = WindowsIdentity.GetCurrent();
            WindowsPrincipal principal = new WindowsPrincipal(identity);
            //判断当前登录用户是否为管理员
            if (principal.IsInRole(WindowsBuiltInRole.Administrator))
            {
                //如果是管理员,则直接运行
                Run(new LoginWindow());
            }
            else
            {
                //创建启动对象
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.UseShellExecute = true;
                startInfo.WorkingDirectory = Environment.CurrentDirectory;
                startInfo.FileName = Assembly.GetExecutingAssembly().Location;
                //设置启动动作,确保以管理员身份运行
                startInfo.Verb = "runas";
                try
                {
                    System.Diagnostics.Process.Start(startInfo);
                }
                catch
                {
                    return;
                }
                //退出
                Application.Current.Shutdown();
            }
        }
    }
}

判断程序是否以管理员运行

using System.Security.Principal;
public bool IsAdministrator()
{
    WindowsIdentity current = WindowsIdentity.GetCurrent();
    WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
    return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
}

开机自启动写入注册表

#region 开机自启动写入注册表

private void RunAtStart()
{
    var starupPath = GetType().Assembly.Location;
    try
    {
        var fileName = starupPath;
        var shortFileName = fileName.Substring(fileName.LastIndexOf('\\') + 1);
        //打开子键节点
        var myReg = Registry.LocalMachine.OpenSubKey(
            "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", RegistryKeyPermissionCheck.ReadWriteSubTree,
            RegistryRights.FullControl);
        if (myReg == null)
        {
            //如果子键节点不存在,则创建之
            myReg = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
        }
        if (myReg != null && myReg.GetValue(shortFileName) != null)
        {
            //在注册表中设置自启动程序
            myReg.DeleteValue(shortFileName);
            myReg.SetValue(shortFileName, fileName);
            LogHelper.WriteLog(typeof(MainWindow), "设置自启动程序操作成功");
        }
        else if (myReg != null && myReg.GetValue(shortFileName) == null)
        {
            myReg.SetValue(shortFileName, fileName);
            LogHelper.WriteLog(typeof(MainWindow), "设置自启动程序操作成功");
        }
    }
    catch
    {
        LogHelper.WriteLog(typeof(MainWindow),"写注册表操作发生错误");
    }
}

#endregion
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Windows 11 操作系统中,在 WPF 应用程序中设置以管理员权限运行并启用系统屏幕上的键盘,可以按照以下步骤进行操作: 1. 在 Visual Studio 中,打开您的 WPF 项目。 2. 找到 `App.xaml` 文件,并将其打开。 3. 在 `App.xaml` 文件中,找到 `Application` 标签,并添加 `requestedExecutionLevel` 属性以设置应用程序的运行级别为管理员。示例代码如下: ```xml <Application x:Class="YourAppName.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:YourAppName" StartupUri="MainWindow.xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib"> <Application.Resources> </Application.Resources> <Application.Manifest> <assemblyManifest> <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3"> <security> <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3"> <requestedExecutionLevel level="requireAdministrator" uiAccess="false" /> </requestedPrivileges> </security> </trustInfo> </assemblyManifest> </Application.Manifest> </Application> ``` 4. 在您想要启用系统屏幕键盘的页面的代码文件中,添加以下引用和代码: ```csharp using System.Diagnostics; private void MainWindow_Loaded(object sender, RoutedEventArgs e) { // 启动系统屏幕键盘 Process.Start("osk.exe"); } ``` 这样,当您的应用程序以管理员权限运行时,将自动启动系统屏幕键盘。 请注意,为了使用 `Process.Start` 方法启动系统屏幕键盘,您可能需要添加对 `System.Diagnostics` 命名空间的引用。 希望这可以帮助您在 Windows 11 上设置以管理员权限运行并启用系统屏幕上的键盘。如果您有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值