0

The framework I am using is .Net 8.

I tried to followed the accepted answer in How to execute Linux command line in C# Mono

And here is my code:

ProcessStartInfo _processStartInfo = new ProcessStartInfo();
_processStartInfo.FileName = "dmesg | grep tty";
_processStartInfo.UseShellExecute = false;
_processStartInfo.RedirectStandardOutput = true;
Process p = Process.Start(_processStartInfo );
var _result = p.StandardOutput.ReadToEnd();
p.WaitForExit();
p.Close();

After I ran the code, it reports this error:

Unhandled exception. System.ComponentModel.Win32Exception (2): An error occurred           trying to start process 'dmesg | grep tty' with working directory '/home/admin'          . No such file or directory
   at System.Diagnostics.Process.ForkAndExecProcess(ProcessStartInfo startInfo,           String resolvedFilename, String[] argv, String[] envp, String cwd, Boolean setCr          edentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32          & stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec)
   at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)

In terminal of putty, the command runs well as below: enter image description here

What's wrong with it?

2
  • Can you run this in a terminal directly? Where is the application currently running? The stacktrace seems to say you are trying that on a windows machine. Commented Apr 17, 2024 at 3:12
  • @TanveerBadar Yes, I can run it successfully in terminal directly. The application is currently running Avalonia (Linux DRM) in Rasbian Lite. I feel strange also that it reports an error about win32 while I never use this. Commented Apr 17, 2024 at 3:25

1 Answer 1

2

Try to use FileName and Arguments like below, it works in my side.

ProcessStartInfo startInfo = new ProcessStartInfo
{
    FileName = "/bin/bash",
    Arguments = "-c \"dmesg | grep tty\"",
    UseShellExecute = false,
    RedirectStandardOutput = true,
    RedirectStandardError = true
};

Here is the test result in my azure app service linux plateform.

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

It works now! BTW, what is the -c truly used for?
Hi @Volfogg -c If the -c option is present, then commands are read from the first non-option argument command_string. If there are arguments after the command_string, they are assigned to the positional parameters, starting with $0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.