alroyso 1 year ago
parent
commit
e261c3a194
1 changed files with 13 additions and 5 deletions
  1. 13 5
      lib/app/utils/shell.dart

+ 13 - 5
lib/app/utils/shell.dart

@@ -18,13 +18,21 @@ Future<void> killProcess(String name) async {
 
 Future<bool> isRunningAsAdmin() async {
   final Shell shell = Shell();
-  // 使用 "net session" 命令,如果成功,则意味着我们有管理员权限
-  final List<ProcessResult> result = await shell.run('net session');
-  if (result.isNotEmpty && result[0].exitCode == 0)  {
-    return true;
-  } else {
+
+  try {
+    // Attempt the "net session" command.
+    final List<ProcessResult> results = await shell.run('net session');
+
+    // Check the exit code of the first result.
+    if (results.isNotEmpty && results[0].exitCode == 0) {
+      return true;
+    }
+  } catch (e) {
+    // If any exception is thrown (like ShellException), we assume we don't have admin rights.
     return false;
   }
+
+  return false;
 }
 
 Future<ProcessResult> runAsAdmin(String executable, List<String> arguments) async {