Aller au contenu

« Process » : différence entre les versions

De Banane Atomic
Aucun résumé des modifications
 
(7 versions intermédiaires par le même utilisateur non affichées)
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]


= Exécuter un fichier ou une commande =
= Run a binary =
{{info | By default the {{boxx|WorkingDirectory}} is the calling directory.}}
<kode lang=csharp>
<kode lang=csharp>
using System.Diagnostics;
using System.Diagnostics;


// en une ligne
// in 1 line
Process.Start("C:\chemin\fichier.exe", "paramètre1 paramètre2").WaitForExit();
Process.Start("/path/folder/binary", "arg1 arg2").WaitForExit();


// ProcessStartInfo?
// with ProcessStartInfo
Process.Start(new ProcessStartInfo("CheminVersLeFichier"));
Process.Start(new ProcessStartInfo("/path/folder/binary", "arg1 arg2")
{
    WindowStyle = ProcessWindowStyle.Hidden
})?.WaitForExit();


// Avec des options
// read synchronously output and error
ProcessStartInfo psi = new ProcessStartInfo("exe", "paramètre1 paramètre2");
var process = Process.Start(new ProcessStartInfo("/path/folder/binary", "arg1 arg2")
psi.WindowStyle = ProcessWindowStyle.Hidden; // masque le processus
{
Process proc = Process.Start(psi);
    RedirectStandardOutput = true,
proc.WaitForExit(); // Attend que le processus se termine
    RedirectStandardError = true,
    UseShellExecute = false  // default value
});


// Lecture de la sortie
ProcessStartInfo psi = new ProcessStartInfo("/chemin/script.sh", "paramètre1 paramètre2");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
// par défaut, le working directory est celui de l'application appelante
// on peut donc le redéfinir pour qu'il corresponde au chemin du script appelé
psi.WorkingDirectory = Path.GetDirectoryName("/chemin/script.sh");
Process process = Process.Start(psi);
string processOutput = process.StandardOutput.ReadToEnd();
string processOutput = process.StandardOutput.ReadToEnd();
process.WaitForExit();
process.WaitForExit();


// écoute d'output et d'error
// read asynchronously output and error
command = command.Replace("\"", "\\\"");
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();
 
</kode>
 
= Execute shell commands =
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>
var process = new Process()
var process = new Process()
{
{
     StartInfo = new ProcessStartInfo
     StartInfo = new ProcessStartInfo
     {
     {
         FileName = "/bin/bash",
         FileName = "borg",
         Arguments = $"-c \"{command}\"",
         Arguments = $"create --verbose --progress --stats --compression lz4 \"{archive}\"::{{now:%Y-%m-%d}} {paths}",
         RedirectStandardOutput = true,
         UseShellExecute = true,
         RedirectStandardError = true,
         WorkingDirectory = "/path/folder"
        UseShellExecute = false,
        CreateNoWindow = true,
     }
     }
};
};
 
// set env var
process.OutputDataReceived += (a, b) => Console.WriteLine(b.Data);
process.StartInfo.EnvironmentVariables["BORG_PASSPHRASE"] = borgPassword;
process.ErrorDataReceived += (a, b) => Console.WriteLine(b.Data);


process.Start();
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
process.WaitForExit();
</kode>
</kode>
{{warn | Le WorkingDirectory du nouveau process sera le même que celui du process appelant et non le chemin vers l'exe du nouveau process.}}


= Lister tous les processus qui utilisent un fichier =
= Lister tous les processus qui utilisent un fichier =

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