From e2569f11e553cd20e0aed1ed71d003bf2953ce2b Mon Sep 17 00:00:00 2001 From: tongg Date: Thu, 27 Feb 2025 22:27:44 +0800 Subject: [PATCH] fix: nvidia driver version check --- App.config | 7 +++- LoginWindow.xaml.cs | 95 +++++++++++++++++++++++++++++++++++++-------- SparkClient.csproj | 1 + 3 files changed, 84 insertions(+), 19 deletions(-) diff --git a/App.config b/App.config index ab3535c..4de78d0 100644 --- a/App.config +++ b/App.config @@ -8,12 +8,15 @@ + + + - + - + diff --git a/LoginWindow.xaml.cs b/LoginWindow.xaml.cs index 7a72000..67139a9 100644 --- a/LoginWindow.xaml.cs +++ b/LoginWindow.xaml.cs @@ -3,6 +3,7 @@ using SparkClient.Model.Helper; using SparkClient.ViewModel.Configuration.SettingsPages; using System.Data; using System.Diagnostics; +using System.Text; using System.Windows; using System.Windows.Input; using Microsoft.Win32; @@ -27,33 +28,93 @@ public partial class LoginWindow : Window private void checkNvidia() { string nvidiaVersion = ""; - const string regPath = @"SOFTWARE\NVIDIA Corporation\Global\NVSMI"; - const string keyName = "DisplayDriverVersion"; - - try + using (Process process = new Process()) { - // 针对 64 位系统访问注册表 - using (var baseKey = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)) - using (var key = baseKey.OpenSubKey(regPath)) + process.StartInfo.FileName = "nvidia-smi"; + process.StartInfo.Arguments = "--query-gpu=driver_version --format=csv,noheader,nounits"; + process.StartInfo.UseShellExecute = false; + process.StartInfo.CreateNoWindow = true; + process.StartInfo.RedirectStandardOutput = true; + process.StartInfo.RedirectStandardError = true; + + // 使用StringBuilder来捕获输出和错误信息 + StringBuilder outputBuilder = new StringBuilder(); + StringBuilder errorBuilder = new StringBuilder(); + + process.OutputDataReceived += (sender, e) => { - if (key != null) + if (e.Data != null) + outputBuilder.AppendLine(e.Data); + }; + process.ErrorDataReceived += (sender, e) => + { + if (e.Data != null) + errorBuilder.AppendLine(e.Data); + }; + + process.Start(); + + // 开始异步读取输出和错误流 + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + process.WaitForExit(); + + // 获取完整的输出和错误信息 + string outputMsg = outputBuilder.ToString(); + string errorMsg = errorBuilder.ToString(); + nvidiaVersion = (string.IsNullOrWhiteSpace(errorMsg)?outputMsg:errorMsg).Trim(); + if (process.ExitCode == 0 || !string.IsNullOrWhiteSpace(nvidiaVersion)) + { + if (CompareVersions(nvidiaVersion, "528.33") <= 0) { - var value = key.GetValue(keyName)?.ToString(); - nvidiaVersion = !string.IsNullOrEmpty(value) ? value : "未找到驱动版本"; + MessageBox messageBox = new MessageBox(); + messageBox.Show(MultilingualHelper.getString("NotNvidiaVersionLow").Replace("%version", nvidiaVersion)); } } + else + { + MessageBox messageBox = new MessageBox(); + messageBox.Show(MultilingualHelper.getString("NotNvidia")); + } } - catch (Exception ex) + } + + public static int CompareVersions(string versionA, string versionB) + { + int[] partsA = ParseVersion(versionA); + int[] partsB = ParseVersion(versionB); + + int maxLength = Math.Max(partsA.Length, partsB.Length); + for (int i = 0; i < maxLength; i++) { - nvidiaVersion = ex.Message; - Logger.Error("Nvidia检测错误:"+ ex); + int a = (i < partsA.Length) ? partsA[i] : 0; + int b = (i < partsB.Length) ? partsB[i] : 0; + + if (a != b) + { + return a.CompareTo(b); + } } - - MessageBox messageBox = new MessageBox(); - messageBox.Show(nvidiaVersion); - + + return 0; // 版本相同 } + private static int[] ParseVersion(string version) + { + string[] stringParts = version.Split('.'); + int[] intParts = new int[stringParts.Length]; + for (int i = 0; i < stringParts.Length; i++) + { + if (!int.TryParse(stringParts[i], out int part)) + { + return [0, 0, 0]; + } + intParts[i] = part; + } + + return intParts; + } private void checkCUDA() { try diff --git a/SparkClient.csproj b/SparkClient.csproj index 058b24d..1bf995e 100644 --- a/SparkClient.csproj +++ b/SparkClient.csproj @@ -14,6 +14,7 @@ 北京跃洋新视科技有限公司 1.0.0 1.0.0 + win-x64