« Process » : différence entre les versions
Apparence
Aucun résumé des modifications |
|||
Ligne 2 : | Ligne 2 : | ||
* [https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.110).aspx ProcessStartInfo] | * [https://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo(v=vs.110).aspx ProcessStartInfo] | ||
= | = Run a binary = | ||
<kode lang=csharp> | <kode lang=csharp> | ||
using System.Diagnostics; | using System.Diagnostics; | ||
// | // in 1 line | ||
Process.Start(" | Process.Start("/path/folder/binary", "arg1 arg2").WaitForExit(); | ||
// ProcessStartInfo | // with ProcessStartInfo | ||
Process.Start(new ProcessStartInfo(" | Process.Start(new ProcessStartInfo("/path/folder/binary", "arg1 arg2") | ||
{ | |||
WindowStyle = ProcessWindowStyle.Hidden | |||
})?.WaitForExit(); | |||
// | // read output and error | ||
var process = Process.Start(new ProcessStartInfo("/path/folder/binary", "arg1 arg2") | |||
{ | |||
RedirectStandardOutput = true, | |||
RedirectStandardError = true, | |||
UseShellExecute = false // default value | |||
}); | |||
string processOutput = process.StandardOutput.ReadToEnd(); | string processOutput = process.StandardOutput.ReadToEnd(); | ||
process.WaitForExit(); | process.WaitForExit(); |
Version du 20 août 2023 à 20:14
Liens
Run a binary
using System.Diagnostics;
// in 1 line
Process.Start("/path/folder/binary", "arg1 arg2").WaitForExit();
// with ProcessStartInfo
Process.Start(new ProcessStartInfo("/path/folder/binary", "arg1 arg2")
{
WindowStyle = ProcessWindowStyle.Hidden
})?.WaitForExit();
// read output and error
var process = Process.Start(new ProcessStartInfo("/path/folder/binary", "arg1 arg2")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false // default value
});
string processOutput = process.StandardOutput.ReadToEnd();
process.WaitForExit();
// écoute d'output et d'error
command = command.Replace("\"", "\\\"");
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c \"{command}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true,
}
};
process.OutputDataReceived += (a, b) => Console.WriteLine(b.Data);
process.ErrorDataReceived += (a, b) => Console.WriteLine(b.Data);
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
|
![]() |
Le WorkingDirectory du nouveau process sera le même que celui du process appelant et non le chemin vers l'exe du nouveau process. |
Execute shell commands
Some shell commands rewrite on the current line, to keep the same visual as the original command, use {{{1}}}.
var processInfo = new ProcessStartInfo
{
FileName = "borg",
Arguments = $"create --verbose --progress --stats --compression lz4 \"{archive}\"::{{now:%Y-%m-%d}} {paths}",
UseShellExecute = true,
WorkingDirectory = "/path/folder"
};
processInfo.EnvironmentVariables["BORG_PASSPHRASE"] = borgPassword;
var process = new Process()
{
StartInfo = processInfo
};
process.Start();
process.WaitForExit();
|
Lister tous les processus qui utilisent un fichier
|