【问题标题】:Starting Program with Admin Rights in C#在 C# 中以管理员权限启动程序
【发布时间】:2013-11-19 15:46:46
【问题描述】:

我最近发现,很难从代码中启动具有管理权限的 Java 进程,因此我决定制作一个单独的启动或调用程序,用 C# 编写。 C# 程序有权以管理员身份运行,但是我怎样才能调用 jar 以作为一个运行呢? 它调用的程序需要管理员权限才能从注册表中设置和检索值,这是它的设置,并且由于我无法在 Java 中以管理员身份启动程序,因此我需要在 C# 中执行此操作(我选择 C# 而不是 VB,因为我以前用 VB 编写代码,但不再喜欢这种语法了)。这是我到目前为止所取得的进展,但我仍然在控制台中收到错误输出:

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();
        }
    }

编辑:这是控制台的输出:

【问题讨论】:

  • 你遇到了什么错误?

标签: c# java admin admin-rights administrative


【解决方案1】:

由于错误表明“用户名/密码错误”... 您正在设置 pr.StartInfo.UserName 但不是 pr.StartInfo.Password

【讨论】:

  • 但它是管理员帐户。它没有密码。这就是困扰我的地方。知道如何解决这个问题吗?
  • 是的,我做到了。但随后 Java 以最少的权限开始,这不是我想要的。
猜你喜欢
  • 2011-05-05
  • 2012-08-03
  • 2016-09-29
  • 1970-01-01
  • 1970-01-01
  • 2013-08-05
  • 2011-06-06
  • 2018-11-21
  • 2018-05-01
相关资源
最近更新 更多