正如我最近发现的那样,很难从代码中启动具有管理权限的Java进程,因此我决定使用C#编写一个单独的启动或调用程序。 C#程序有权以管理员身份运行,但是如何调用jar作为一个运行? 它调用的程序需要管理员权限来设置和检索注册表中的值,这是它的设置,因为我无法以Java身份启动程序作为管理员,我需要在C#中这样做(我选择了C#over 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();
}
}
编辑:这是控制台的输出:
答案 0 :(得分:1)
由于错误表示“用户名/密码错误”...
您正在设置pr.StartInfo.UserName
但不是pr.StartInfo.Password