Can a path whose name contains blank spaces be formatted in CMD?

2

The problem is that I need to execute a command in CMD, but since the paths of the files I search in Windows contain blank spaces, it requires me to use double quotes ("") to separate the paths.

When I call it from C # I try to "escape" the paths to embed the double quote, trying with @" "ruta" " and " \"ruta\" " . But it turns out that the final string drags the diagonal \"ruta\" and is incorrect for the CMD interpreter.

Can I tell CMD to look for routes without using double quotes or how can I format the route for the command I prepare to send from C #?

The command to execute is similar to:

C:/Users/user/Documents/Sistema de Calidad/OfficeToPDF.exe 
C:/Users/user/Documents/Sistema de Calidad/ + archivoEntrada
C:/Users/user/Documents/Sistema de Calidad/ + archivoSalida

It is worth mentioning that I can not rename the folders, because the project is very advanced and if I solve it locally it will also work on the production server whose folders also have spaces in their names.

Update:

The code that I left as tentative is the following.

string consulta = @"""C:/Users/user/Documents/Sistema de Calidad/bin/OfficeToPDF.exe"" " +
                                          @" ""C:/Users/user/Documents/Sistema de Calidad/" + ruta + @""" " +
                                          @" ""C:/Users/user/Documents/Sistema de Calidad/" + rutaSalida + @""" ";

EjecutarComando(consulta);

and Run command contains:

 private void EjecutarComando(string comando)
        {
            try
            {
                var proc = new Process
                {
                    StartInfo = new ProcessStartInfo
                    {
                        FileName = "cmd.exe",
                        Arguments = "/c " + comando,
                        UseShellExecute = false,
                        RedirectStandardOutput = true,
                        CreateNoWindow = true
                    }
                };
                proc.Start();
                while (!proc.StandardOutput.EndOfStream)
                {
                    string line = proc.StandardOutput.ReadLine();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

Greetings and thanks.

    
asked by codefactory256 22.03.2018 в 02:01
source

1 answer

0

Since you have your files under the same folder you can modify the CurrentDirectory variable so that it points to there and so you do not even have to use the full path. So would the code:

string archivo1 = "...."; //Parámetro 1
string archivo2 = "...."; //Parámetro 2
string currentDir = Environment.CurrentDirectory;
currentDir = @"C:\Users\user\Documents\Sistema de Calidad\";
//Creo un nuevo proceso desde el cual voy a ejecutar el comando del cmd
Process p = new Process();
//Asigno la información de lo que voy a ejecutar, en este caso es la app y sus parámetros
//Agregué las comillas en caso de que el archivo contenga espacios
p.StartInfo = new ProcessStartInfo("OfficeToPDF.exe", string.Format("\"{0}\" \"{1}\"", archivo1, archivo2));
p.Start();
//Reestablezco el valor de la variable CurrentDir
Environment.CurrentDirectory = currentDir;
    
answered by 22.03.2018 / 14:32
source