CommandLineEncoder.EncodeArgText C# (CSharp) Метод

EncodeArgText() публичный статический Метод

public static EncodeArgText ( string original ) : string
original string
Результат string
    public static string EncodeArgText(string original)
    {
        var result = original;
            result = TryEncodeNewLine(result);
            result = TryEncodeSlashesFollowedByQuotes(result);
            result = TryEncodeQuotes(result);
            result = TryEncodeLastSlash(result);
            return "\"" + result + "\"";
    }

Usage Example

Пример #1
0
    // Executes file_name without creating a Window and without using the shell
    // with the parameters. Waits that it finishes it. Returns the stdout and stderr.
    private static Result runDo(string file_name, List <string> parameters)
    {
        Process          process          = new Process();
        ProcessStartInfo processStartInfo = new ProcessStartInfo();

        processStartInfo.FileName = file_name;

        string parameters_string = "";

        foreach (string parameter in parameters)
        {
            parameters_string += CommandLineEncoder.EncodeArgText(parameter) + " ";
        }

        processStartInfo.Arguments = parameters_string;

        LogB.Debug("ExecuteProcess FileName: " + processStartInfo.FileName);
        LogB.Debug("ExecuteProcess Arguments: " + processStartInfo.Arguments);

        processStartInfo.CreateNoWindow         = true;
        processStartInfo.UseShellExecute        = false;
        processStartInfo.RedirectStandardInput  = false;
        processStartInfo.RedirectStandardError  = true;
        processStartInfo.RedirectStandardOutput = true;

        process.StartInfo = processStartInfo;

        try {
            process.Start();
        }
        catch (Exception e) {
            string errorMessage = String.Format(Catalog.GetString("Cannot start:\n" +
                                                                  "{0}\n" +
                                                                  "with the parameters:" +
                                                                  "{1}\n" +
                                                                  "Exception:\n" +
                                                                  "{2}"),
                                                processStartInfo.FileName, parameters_string, e.Message);
            LogB.Warning(errorMessage);
            return(new Result("", "", Result.ERROR_CANT_START, errorMessage));
        }

        string stdout = process.StandardOutput.ReadToEnd().TrimEnd('\n');
        string stderr = process.StandardError.ReadToEnd().TrimEnd('\n');

        process.WaitForExit();

        if (stderr != "")
        {
            LogB.Warning(String.Format("Executed: {0} Parameters: {1} Stdout: {2} Stderr: {3}", processStartInfo.FileName, parameters_string, stdout, stderr));
        }

        int exitCode = process.ExitCode;

        return(new Result(stdout, stderr, exitCode));
    }
All Usage Examples Of CommandLineEncoder::EncodeArgText