简体   繁体   English

在C#中以管理员权限启动程序

[英]Starting Program with Admin Rights in C#

as I recently found out, it's hard to start a Java process with administrative rights from code, so I decided to make a separate startup or calling program, written in C#. 正如我最近发现的那样,很难从代码中启动具有管理权限的Java进程,因此我决定制作一个单独的使用C#编写的启动或调用程序。 The C# program has the rights to run as an administrator, but how can I call the jar to run as one, though? C#程序具有以管理员身份运行的权限,但是我怎么能将ja​​r作为一个人运行呢? The program it's calling requires admin rights to set and retrieve values from the registry, which are its settings, and as I can't start the program as an admin in Java, I need to do so in C# (I chose C# over VB, because I used to code in VB, but don't like the syntax anymore). 它所调用的程序需要管理员权限才能从注册表中设置和检索值,这是它的设置。由于我无法以Java管理员身份启动该程序,因此需要在C#中执行此操作(我选择了V#上的C#因为我曾经用VB编写代码,但现在不再喜欢这种语法了)。 This is how far I've gotten so far, but I still get an error output in the console: 这是我到目前为止所走的路,但是我仍然在控制台中收到错误输出:

static void Main(string[] args)
    {
        Console.Title = "Universal Android Toolkit Bootup...";
        Console.ForegroundColor = ConsoleColor.Green;

        try
        {
            Process pr = new Process();
            String uat = Environment.CurrentDirectory + "/com.m4gkbeatz.Java.AndroidToolkit.jar";

            pr.StartInfo.Arguments = "-jar " + uat;
            pr.StartInfo.FileName = "java";
            pr.StartInfo.CreateNoWindow = true;
            pr.StartInfo.ErrorDialog = true;
            pr.StartInfo.RedirectStandardOutput = true;
            pr.StartInfo.RedirectStandardError = true;
            pr.StartInfo.UserName = "admin";
            pr.StartInfo.UseShellExecute = false;

            StreamReader str;
            pr.Start();
            str = pr.StandardOutput;

            while (!pr.HasExited)
            {
                Console.WriteLine(str.ReadLine());
            }
        }
        catch (Exception ex)
        {
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine("There was an error while attempting to execute Universal Android Toolkit");
            Console.WriteLine(ex.ToString());
            error = ex.ToString();
            Console.WriteLine("\n");
            Console.WriteLine("Please try again.\nPress ENTER to continue...");
            Console.Read();
            ErrorMenu();
        }
    }

Edit: This is the console's output: 编辑:这是控制台的输出: 控制台错误输出

由于错误指示“用户名/密码错误” ...您正在设置pr.StartInfo.UserName但未设置pr.StartInfo.Password

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM