Browse Source

以管理员权限启动sonopost程序

finlay 10 months ago
parent
commit
9621cce954
3 changed files with 30 additions and 20 deletions
  1. 5 0
      fis/App.axaml.cs
  2. 24 19
      fis/Helpers/SonopostAppHelper.cs
  3. 1 1
      fis/app.manifest

+ 5 - 0
fis/App.axaml.cs

@@ -47,6 +47,11 @@ namespace fis
                 Environment.Exit(0);
                 return;
             }
+
+            //if (!SonopostAppHelper.IsRunAsAdmin()) {
+            //    SonopostAppHelper.RunAsAdmin();
+            //}
+
             if (!SonopostAppHelper.IsExistSonopostApp())   ///ĆôśŻÄ§şĐApp
             {
                 SonopostAppHelper.StartSonopostApp();

+ 24 - 19
fis/Helpers/SonopostAppHelper.cs

@@ -3,6 +3,8 @@ using System.Collections.Generic;
 using System.Diagnostics;
 using System.IO;
 using System.Linq;
+using System.Reflection;
+using System.Security.Principal;
 using System.Text;
 using System.Threading.Tasks;
 
@@ -14,7 +16,8 @@ namespace fis.Win.Dev.Helpers
         /// 检查魔盒软件是否已经启动
         /// </summary>
         /// <returns></returns>
-        public static bool IsExistSonopostApp() {
+        public static bool IsExistSonopostApp()
+        {
             var existApp = Process.GetProcessesByName("Sonopost");
             return existApp.Any();
         }
@@ -33,10 +36,11 @@ namespace fis.Win.Dev.Helpers
                     StartInfo =
                         {
                                 FileName = sonopostPath,
-                                CreateNoWindow = true,
-                                UseShellExecute = false,
-                                RedirectStandardOutput = true,
-                                RedirectStandardError = true,
+                                //CreateNoWindow = true,
+                                UseShellExecute = true,
+                                //RedirectStandardOutput = true,
+                                //RedirectStandardError = true,
+                                Verb = "runas",
                         }
                 };
                 process.Start();
@@ -48,26 +52,27 @@ namespace fis.Win.Dev.Helpers
             }
 
         }
-    
+
 
 
         public static bool CloseSonopostApp()
         {
-         
-                Process[] processes = Process.GetProcessesByName("Sonopost");
-                if (processes.Length > 0) { 
+
+            Process[] processes = Process.GetProcessesByName("Sonopost");
+            if (processes.Length > 0)
+            {
                 Process process = processes[0];
-                    try
-                    {
-                        process.Kill();
-                        process.WaitForExit();
-                    }
-                    catch (Exception)
-                    {
-                        return false;
-                    }
-                  
+                try
+                {
+                    process.Kill();
+                    process.WaitForExit();
                 }
+                catch (Exception)
+                {
+                    return false;
+                }
+
+            }
             return true;
         }
     }

+ 1 - 1
fis/app.manifest

@@ -16,7 +16,7 @@
             如果你的应用程序需要此虚拟化来实现向后兼容性,则移除此
             元素。
         -->
-				<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
+				<requestedExecutionLevel level="asInvoker" uiAccess="false" />
 			</requestedPrivileges>
 		</security>
 	</trustInfo>