Question

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#. The C# program has the rights to run as an administrator, but how can I call the jar to run as one, though? 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). 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: Console Error Output

Was it helpful?

Solution

As error indicates "bad username/password"... you're setting pr.StartInfo.UserName but not pr.StartInfo.Password

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top