Flutter 桌面应用开发之读写Windows注册表

文章目录

需求来源

毛玻璃效果配置选项:

WindowEffect.acrylic:Windows 10 1803 及以上版本

WindowEffect.aero:Windows 10 1803以下版本

书接上回,毛玻璃效果配置选项依据Windows的版本号而有所不同,那么如何获取Windows的版本号了?

Windows查询Windows版本号方法

1. 如何查看Windows版本号

要获取Windows版本号,可以按照以下步骤进行操作:

(1)打开"运行"对话框,可以使用快捷键Win + R。

(2)在运行对话框中输入"winver",然后点击"确定"按钮。

(3)弹出的窗口中会显示Windows的版本号和版本信息。

2. Windows开发如何通过代码查询Windows版本号

(1) 使用C#代码:

C 复制代码
using System;
using Microsoft.Win32;
class Program
   {
       static void Main(string[] args)
       {
           string version = string.Empty;
           using (RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion"))
           {
               if (key != null)
               {
                   version = key.GetValue("CurrentVersion").ToString();
               }
           }
           Console.WriteLine("Windows版本号:" + version);
       }
   }

(2) 使用VB.NET代码

vbnet 复制代码
Imports Microsoft.Win32
Module Module1
      Sub Main()
          Dim version As String = String.Empty
          Using key As RegistryKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion")
              If key IsNot Nothing Then
                  version = key.GetValue("CurrentVersion").ToString()
              End If
          End Using
          Console.WriteLine("Windows版本号:" & version)
      End Sub
  End Module

通过C#和 .NET获取Windows版本的方法,可以总结出2个关键点:
1. 引入Win32依赖库;
2. 读取注册表信息

3.通过注册表查看Windows版本信息

打开注册表编辑器,输入"计算机\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion",就可以查看版本号信息

Flutter查询Windows版本号方法

Windows原生开发获取版本号的方法为Flutter提供了指导,只要可以找到读写windows注册表,就可以实现版本号的查询。目前,flutter 已经提供了读写windows注册表的依赖库,很方便我们实现相关功能.

依赖库

win32_registry

支持平台

Windows

实现步骤

1. 在pubspec.yaml中添加依赖

dart 复制代码
dependencies:
  ...
  win32_registry: ^1.1.2

2. 获取版本号

dart 复制代码
import 'package:win32_registry/win32_registry.dart';

int? getWindowBuildNumber() {
  const regCurrentVersionKey = r'SOFTWARE\Microsoft\Windows NT\CurrentVersion';
  final key =
      Registry.openPath(RegistryHive.localMachine, path: regCurrentVersionKey);
  final mCurrentVersion = key.getValueAsString('CurrentVersion');
  if (kDebugMode) {
    print("mCurrentVersion:$mCurrentVersion");
  }
  final mUBR = key.getValueAsInt('UBR');
  if (kDebugMode) {
    print("mUBR:$mUBR");
  }
  return mUBR;
}

打印值:

dart 复制代码
flutter: mCurrentVersion:6.3
flutter: mUBR:1081

方法解释

openPath

打开指定的注册表键

dart 复制代码
RegistryKey openPath(
  RegistryHive hive, {
  String path = '',
  AccessRights desiredAccessRights = AccessRights.readOnly,
})

RegistryHive :注册表的入口,

枚举值有localMachine(HKEY_LOCAL_MACHINE),

currentUser(HKEY_CURRENT_USER),

allUsers(HKEY_USERS),

classesRoot(HKEY_CLASSES_ROOT),

currentConfig(HKEY_CURRENT_CONFIG),

performanceData(HKEY_PERFORMANCE_DATA);

除了performanceData以外,其他的值与注册表一级目录一一对应;

path :注冊表的路径
desiredAccessRights :期望权限

readOnly(KEY_READ),

writeOnly(KEY_WRITE),

allAccess(KEY_ALL_ACCESS);

createKey:创建一个新的注册表键

下面是通过注册表添加程序自动启的案例

dart 复制代码
Future<void> updateRegistryTest()  {
  const regCurrentVersionKey = r'Software\Microsoft\Windows\CurrentVersion\Run';
  final key =
  Registry.openPath(RegistryHive.currentUser, path: regCurrentVersionKey,desiredAccessRights: AccessRights.writeOnly);
  key.createKey("WeChat");
  key.createValue(const RegistryValue("WeChat", RegistryValueType.string, "D:\\Program Files (x86)\\Tencent\\WeChat\\WeChat.exe"));
  key.close();
}

创建结果如下:

getValue

查询指定键的值

getValueAsString

查询字符串类型的指定键的值

getValueAsInt

查询Int类型的指定键的值

createValue

设置指定键的值

deleteValue

删除指定键的值

deleteKey

删除指定的注册表键

renameSubkey

更改指定注册表项的名称

注意事项

  1. Unhandled Exception: Error 0x80070005: 拒绝访问
bash 复制代码
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Error 0x80070005: 拒绝访问。
#0      Registry.openPath (package:win32_registry/src/registry.dart:56:7)
#1      getWindowBuildNumber (package:window_example/util/window_util.dart:61:16)
#2      showAcrylic (package:window_example/util/window_util.dart:35:22)
#3      _MyHomePageState._init (package:window_example/main.dart:142:5)
<asynchronous suspension>

出现该异常:需要检查路径是否正确,权限参数是否正确;

拓展话题

Windows注册表介绍与操作

相关推荐
Vex2une6 分钟前
系统出问题不想重装?系统还原点创建与重置此电脑完整指南
windows·系统还原点·重置此电脑·系统还原怎么用·创建还原点·保留文件重置·系统保护开启
黑科技iOS上架3 小时前
iOS4.3合规自测手段:二进制分析
flutter·ios·cocoa·审核
好的佩奇18 小时前
蓝牙之 GATT 数据模型
android·flutter·蓝牙
保卫大狮兄1 天前
2026中小制造企业轻量化ERP选型观察:产供销协同路线、上线周期与ROI分析
windows·microsoft·erp·生产管理
陈工大模型1 天前
MacOS 跨平台压缩包处理:从编码痛点到工程化落地的完整方案
windows·macos·缓存·电脑·策略模式·c盘清理
凌镜icecraft2 天前
我改文件扩展名的时候那个警告弹窗再也不会弹出来了
windows·c#·wpf·explorer
the3clipse2 天前
Windows 11 26H2 AI 功能使用指南:回顾、单击以执行与实时字幕
人工智能·windows·copilot+ pc·recall 回顾·单击以执行·实时字幕翻译·windows studio
王二蛋与他的张大花2 天前
后台内存 827MB,我一行业务逻辑都没改就砍到 100MB
android·flutter·性能优化
the3clipse2 天前
Windows 11 26H2 账户登录与 Windows Hello 指南:并行验证、增强登录安全与无密码设置
windows·无密码登录·windows hello·增强登录安全性ess·pin与密码区别·动态锁·win11 26h2
承渊政道2 天前
Toonflow AI短剧教程:Windows部署、批量生成与角色一致性
人工智能·windows·cpolar·ai短剧·toonflow