ZW.Huang
ZW.Huang

Reputation: 71

.Net 5 WorkerService Run Other APP Not Work Well

I setting my WorkerService appsettings.json like below

"AppRun": {
    "App1": {
      "AppName": "CheckDataForm",
      "AppPath": "D:\\2021-Project\\Project\\CheckDataForm-MSSQL\\CheckDataForm\\bin\\Debug\\net5.0-windows\\CheckDataForm.exe"
    },
    "App2": {
      "AppName": "notepad++",
      "AppPath": "C:\\Program Files\\Notepad++\\notepad++.exe"
    },

and call the app like this:

          //i is foreach count
                   
             var AppName = _config["AppRun:App"+i+":AppName"];
             var AppPath = _config["AppRun:App" + i + ":AppPath"];

             //check file exist
             var fileExist = System.IO.File.Exists(AppPath);
          
            if ( !String.IsNullOrEmpty (AppName) && !String.IsNullOrEmpty(AppPath) && 
                fileExist )
            {
                //find APP
                var processApp = Process.GetProcessesByName(AppName);

                //can't find app
                if (processApp.Length <=0 )
                {
                    try
                    {
                        Process proc = new Process();
                        proc.StartInfo.FileName = AppPath;
                        proc.StartInfo.UseShellExecute = true;
                        proc.StartInfo.Verb = "runas";
                        proc.Start();


                    }
                    catch (Exception ex)
                    {
                        var error = ex;
                    }



                }
   
            }

the Process can Start notepad++ , but can't start CheckDataForm .net core windowsForm app, and no exception ,

(I have added to RegistryKey , .netframework 4.8 Ver work well , but .net core 5 Ver have no any Responds and no error)

but I can Manual to run CheckDataForm , and work well, have any idea to fix it, thank you

Upvotes: 1

Views: 51

Answers (1)

dotnetstep
dotnetstep

Reputation: 17485

If I make assumption that you are not able to load reference when running .net core application then you have to add following line overthere.

Process proc = new Process();
                        proc.StartInfo.FileName = AppPath;
                        proc.StartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(AppPath);
                        proc.StartInfo.UseShellExecute = true;
                        proc.StartInfo.Verb = "runas";
                        proc.Start();

You have to set WorkingDirectory.

Upvotes: 2

Related Questions