« Process » : différence entre les versions
Apparence
Ligne 48 : | Ligne 48 : | ||
Some shell commands rewrite on the current line, to keep the same visual as the original command, use {{boxx|1=UseShellExecute = true}}. | Some shell commands rewrite on the current line, to keep the same visual as the original command, use {{boxx|1=UseShellExecute = true}}. | ||
<kode lang=csharp> | <kode lang=csharp> | ||
var process = new Process() | var process = new Process() | ||
{ | { | ||
StartInfo = | StartInfo = new ProcessStartInfo | ||
{ | |||
FileName = "borg", | |||
Arguments = $"create --verbose --progress --stats --compression lz4 \"{archive}\"::{{now:%Y-%m-%d}} {paths}", | |||
UseShellExecute = true, | |||
WorkingDirectory = "/path/folder" | |||
} | |||
}; | }; | ||
// set env var | |||
process.StartInfo.EnvironmentVariables["BORG_PASSPHRASE"] = borgPassword; | |||
process.Start(); | process.Start(); |
Dernière version du 27 août 2023 à 10:48
Liens
Run a binary
![]() |
By default the WorkingDirectory is the calling directory. |
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 synchronously 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();
// read asynchronously output and error
var process = Process.Start(new ProcessStartInfo("/path/folder/binary", "arg1 arg2")
{
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false // default value
});
process.OutputDataReceived += (a, b) => Console.WriteLine(b.Data);
process.ErrorDataReceived += (a, b) => Console.WriteLine(b.Data);
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
|
Execute shell commands
Some shell commands rewrite on the current line, to keep the same visual as the original command, use UseShellExecute = true.
var process = new Process()
{
StartInfo = new ProcessStartInfo
{
FileName = "borg",
Arguments = $"create --verbose --progress --stats --compression lz4 \"{archive}\"::{{now:%Y-%m-%d}} {paths}",
UseShellExecute = true,
WorkingDirectory = "/path/folder"
}
};
// set env var
process.StartInfo.EnvironmentVariables["BORG_PASSPHRASE"] = borgPassword;
process.Start();
process.WaitForExit();
|
Lister tous les processus qui utilisent un fichier
|