0

I want to start a process with some arguments out of a .NET 6 console application to run a dll that was also created in .NET 6.

When I try to cmd: > dotnet myPath/myApp.dll testParam everything works fine

but when I try to use:

Process process = new Process();
        process.StartInfo.FileName = "dotnet myPath/myApp.dll";
        process.StartInfo.WorkingDirectory = "myPath";
        process.StartInfo.Arguments = "testParam";
        process.StartInfo.UseShellExecute = false;
        process.Start();

I'm getting the following exception

System.ComponentModel.Win32Exception: 'An error occurred trying to start process 'dotnet myPath/myApp.dll testParam' with working directory 'myPath'.

As I try to copy and paste the string out of the exception into cmd, it works just fine.

I tried to set the working directory, as explained here

KUMI
  • 39
  • 5
  • my path should have valid path and been given as a sample in the article. – Kamran Shahid Dec 13 '22 at 07:37
  • How is "dotnet myPath/myApp.dll testParam" a file name? Whatever your usual commandline would be, the part before the first space is the file name and the rest is the arguments. – jmcilhinney Dec 13 '22 at 07:37
  • Why are you calling `Path.Combine` and passing one value? How do you combine one value? – jmcilhinney Dec 13 '22 at 07:38
  • You should start the cmd.exe process and pass the dotnet stuff as parameters of that process with /K or /C option. Not sure, but you can also try to use "dotnet.exe" to make clear that is an executable that you want to run – Steve Dec 13 '22 at 07:39
  • @jmcilhinney fixed it. It's a combined in my project but i simplified the path for this example code – KUMI Dec 13 '22 at 07:43
  • @KamranShahid I double checked the string out of the exception that was thrown. Works with copy and paste into cmd. I just simplified the long string for the example – KUMI Dec 13 '22 at 07:44
  • In any case, the FileName property should be ONLY the name of the executable that you want to run, nothing else. All the other stuff should go in the Arguments property – Steve Dec 13 '22 at 07:46
  • @Steve absolutely no reason to call cmd.exe to call dotnet.exe to run the DLL. – mason Dec 13 '22 at 07:49

2 Answers2

2

Please see the documentation https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process?view=net-6.0

var dllPath = Path.Combine("myPath", "myApp.dll");

using Process process = new Process();
process.StartInfo.FileName = "dotnet"; // Append ".exe" if on windows
process.StartInfo.Arguments = $"{dllPath} testParam";
process.StartInfo.UseShellExecute = false;
process.Start();
m_j_alkarkhi
  • 337
  • 4
  • 14
-2

I've run into the same issue when using .NET

The solution for me was to use this extension method. It is to open URLs in the browser but it might work for any exe as well. Just give it a try.

public static void OpenUrl(string url)
{
    try
    {
        Process.Start(url);
    }
    catch
    {
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            url = url.Replace("&", "^&");
            Process.Start(new ProcessStartInfo("cmd", $"/c start {url}") { CreateNoWindow = true });
        }
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
        {
            Process.Start("xdg-open", url);
        }
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
        {
            Process.Start("open", url);
        }
        else
        {
            throw;
        }
    }
}
Marvin Klein
  • 1,436
  • 10
  • 33
  • 1
    _"I've run into the same issue when using .NET...it might work for any exe as well. Just give it a try"_ - This doesn't answer the question and reads like a _"me too!"_ answer. As mentioned in the comments above, the OP in this case does **not** need to `ProcessStartInfo("cmd", ...` –  Dec 13 '22 at 07:59