Command linux on C#

Questions à propos du scripting. Hors Shader, GUI, Audio et Mobile.
Warcza
Messages : 160
Inscription : 18 Fév 2016 20:49

Command linux on C#

Message par Warcza » 16 Déc 2021 12:23

Bonjour,

J'ai un petit problème pour exécuter une commande linux depuis un script c#.
J'aimerais pouvoir lancer un "fichier" depuis le c# (qui correspond du coup à un logiciel), du coup j'essais de lancer 2 commandes:

La première étant de pouvoir autoriser le fichier à être un fichier exécutable en faisant :

Code : Tout sélectionner

 private void ChmodCommand(string filePath)
        {
            var escapedArgs = filePath.Replace("\"", "\\\"");

            var process = new Process()
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "/bin/bash", 
                    Arguments = $"-c chmod +x \"{escapedArgs}\"",
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                }
            };

            process.Start();
            string result = process.StandardOutput.ReadToEnd();
            process.WaitForExit();
        }
Et de lancer ce même fichier avec :

Code : Tout sélectionner

     public string Execute(string filePath)
        {
            var escapedArgs = filePath.Replace("\"", "\\\"");

            var process = new Process()
            {
                StartInfo = new ProcessStartInfo
                {
                    FileName = "/bin/bash", 
                    Arguments = $"-c \"{escapedArgs}\"",
                    RedirectStandardOutput = true,
                    UseShellExecute = false,
                    CreateNoWindow = true,
                }
            };

            process.Start();
            string result = process.StandardOutput.ReadToEnd();
            process.WaitForExit();

            return result;
        }
Et que ce soit la premiere ou deuxieme commande j'ai une erreur
" Win32Exception: ApplicationName='/bin/bash', CommandLine='-c chmod +x "/home/etc...,
CurrentDirectory='', Native error= mono-io-layer-error (0) at System.Diagnostics.Process.StartWithCreateProcess (System.Diagnostics.ProcessStartInfo startInfo) [0x00000] in <00000000000000000000000000000000>:0" etc....

Donc voila, si quelqu'un a une idée ou des pistes, je suis preneur,
Merci bien!
(╯°□°)╯︵ ┻━┻

Répondre

Revenir vers « Scripting »