I’m writing a C# code in Visual Studio Code on Windows 11 to run an application from my WSL Ubuntu and saves the output as a string file.
I’ve already installed WSL and Ubuntu. When I write the following commands directly on my WSL Ubuntu terminal, they produce the expected results.
root@DESKTOP-JCGMG2B:~# cd ENHSP-Public/
root@DESKTOP-JCGMG2B:~/ENHSP-Public# java -jar enhsp-dist/enhsp.jar -o Domain-numerical.pddl -f Problem-numerical.pddl -ha true -s wastar -h hmrp -wh 4
To run these two commands from my VSCode, I wrote the following function:
public static string RunWSLCommand(string command)
{
var startInfo = new ProcessStartInfo
{
FileName = "wsl",
Arguments = $"-d Ubuntu -e "{command}"",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = new Process
{
StartInfo = startInfo
};
process.Start();
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
return output;
}
Unfortunately, I’m only getting output when I use the "ls"
command. For example, the command
"cd ~; ls"
doesn’t produce any output.
Can anyone help me with this? How can I correctly execute my application from Ubuntu using this C# function?
Thank you.
I have also tried to give a bash file as input. But it doesn’t work and I don’t understand why.
Shermin Sherkat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.