Link to home
Start Free TrialLog in
Avatar of largeone
largeone

asked on

Spaces in Paths as Command Line Args (C#)

I have a windows service that calls an external "updater" .exe.  I pass 3 args to the updater.  The problem I'm having is that the second and third and are both paths that almost always contain whitespace characters.  The updater app is always comparing files in "C:\Program Files\...\MyDir\ with files on a network share .that we have yet to establish.  

The code is below, but the simple problem is that Void Main(args[]) sees "C:\Program" and "Files\...\MyDir\" as separate arguments because of the space in the path.  Is there a way to escape that space and pass these two arguments as complete paths?  Seems like this .NET world we're in should have devised a way around the old 8.3 naming conventions.
 
The code in my service:
 string Update_Path=@"C:\Projects\C#\MyUpdatedProject\"); (this will be a UNC in production)  
 string Current_Path= @"C:\Program Files\MyCurProject\");
if(true)
  UpdateMe("MyServiceName", Current_Path, Update_Path);

private void UpdateMe(string ServiceName, string CurPath, string UpdatePath)
        {          
         
            Process proc = new Process();

            string args = ServiceName + " " +  @CurPath + " " + @UpdatePath + "";
            proc.StartInfo.FileName = CurPath + WinServiceUpdater.exe";
            proc.StartInfo.Arguments = args;
            try
            {
                proc.Start();
            }
            catch (System.InvalidOperationException IOex)
            {
                Debug.WriteLine(IOex.Message);
            }

            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

        }


WinServiceUpdater.exe:


static void Main(string[] args)
        {
            if (args.Length == 0)
                return;
            string svcName = args[0];            //Name of service to update (will stop and start with this name)
            string curPath = args[1];               //Directory Path of Current install files
            string updatePath = args[2];         //Path Where Update files sit
            DirectoryInfo UpdateSource = new DirectoryInfo(updatePath);
            DirectoryInfo CurentFiles = new DirectoryInfo(curPath);
           
            string debugmessage = "";
           
            System.ServiceProcess.ServiceController target = new System.ServiceProcess.ServiceController(svcName);

            if(target.Status == System.ServiceProcess.ServiceControllerStatus.Running)
                target.Stop();
            while (target.Status != System.ServiceProcess.ServiceControllerStatus.Stopped)
            {
                target.Refresh();
            }
            Thread.Sleep(2000);

            foreach (FileInfo updateFile in UpdateSource.GetFiles("*.*"))
            {
                if (File.Exists(curPath + updateFile.Name))
                {
                    debugmessage += "<br/>About to Delete: " + curPath + updateFile.Name;
                    //File.Delete(curPath + "\\" + updateFile.Name);

                    debugmessage += "<br/>About to Copy From: " + updatePath + updateFile.Name + " TO: " + curPath + updateFile.Name;
                    //File.Copy(updatePath + "\\" + updateFile.Name, curPath + "\\" + updateFile.Name);
                 
                }
                else
                {
                    debugmessage += "<br/>Does Not Exist: " + curPath + updateFile.Name;
                }            
            }

            debugmessage += "<br/><br/><br/><br/>target.status = " + target.Status.ToString();
            EmailUtil.SendMessage(debugmessage);
            target.Start();
        }
    }
SOLUTION
Avatar of Craig Wagner
Craig Wagner
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of largeone
largeone

ASKER

I did that already.  Maybe I just didn't do it correctly.  The paths are retrieved from registry keys, and then I have to concat them in order to set the Process.StartInfo.Arguments Property.  

It used to look like this, but I changed it back:

string args = ServiceName + " " + "" + @CurPath + "" + " " + "" + @UpdatePath +"";
     
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I should add this:
using "abc" + "" + "def" is really just adding an empty string. So the result of that is just "abcdef".

When you need to add special characters, you need to use the "\" escape character, as:
\"
\t
\n
etc.

Jim

Thanks for the input guys.  For posterity's sake, the actual string that works for my purposes needed to have the ending backslashes doubled up and escaped, so the line of code looks like this:

string args = ServiceName + " " + "\"" + @CurPath + "\\\"" + " " + "\"" + @UpdatePath + "\\\"";

Points split cause neither of you was wrong.
Hi, Is there any other way available other than giving path within double quotes? In VB6, the path is treated as single argument even there are spaces within path, irrespective of adding quote or not. Please let me know your inputs.
Thanks
Samson.