How to run a .EXE in an Azure App Service

2020-04-10 03:44发布

问题:

I have an MVC .NET application, and I want to run some .exes on the server. The exes are jarsigner.exe and zipalign.exe, used to modify and re-sign an android APK. I want to run them from the Controller.

It works locally, using a Process to launch the application, but I have cheated and used hardcoded paths to the .exe and to the folder with the stuff to be used by the exe.

I've added the .exes to the top-level of my project in visual studio, and added a folder containing the files to be worked upon by the .exes.

I'm struggling to workout how I get the path to the exes, and to the folder of files. Once I have that I can then invoke the Process (I suspect I might hit permissions trouble, but one step at a time...).

var processInfo = new ProcessStartInfo(@"C:\jarsigner.exe", @"..arguments"){
 CreateNoWindow = true, UseShellExecute = false
};

回答1:

I'm struggling to workout how I get the path to the exes, and to the folder of files.

If your files under the top-level of project. We can find the path by using Server.MapPath(@"~\Jar\TextFile1.txt"). For jarsigner.exe. It’s in the bin folder of your java JDK. So we can use the environment variable.

Here is the sample code to get the path of jarsigner.exe and running result.

//string path = Server.MapPath(@"~\Jar\TextFile1.txt");    //get file path on the top-level of project(eg. ~\folder\xxx)
        string JavaPath = Environment.GetEnvironmentVariable("JAVA_HOME", EnvironmentVariableTarget.Machine); 
        if(string.IsNullOrEmpty(JavaPath)){
         JavaPath = Environment.GetEnvironmentVariable("JAVA_HOME", EnvironmentVariableTarget.User);
        }
        string path = JavaPath + @"\bin\jarsigner.exe";

        var processInfo = new ProcessStartInfo()
        {
            FileName = path,
            CreateNoWindow = true,
            UseShellExecute = false,
            WindowStyle = ProcessWindowStyle.Hidden
        };

        Process proc = Process.Start(processInfo);
        proc.WaitForExit();
        if (proc.ExitCode == 0)
            ViewBag.Message = path + " exec success.";
        else
            ViewBag.Message = path + " exec fail.";

        return View();