[Tip]Always Quote the commandline arguments that can possibly contain whitespaces

I recently met a issue that: when starting our app by command line way (passing several arguments to the app), on Win7, the specified file can always be opened after lanuching our app, while on XP, the file is always NOT able to be opened! The final reason is that: for arguments that contain whilespaces, when starting a process with it passed in, on XP, it will split it by whilespaces so no valid file can be found so that file won't be opened; while on Win7, it can always be treated as one argument. Whatever, to mark a argument explicitly, always quote it. See below sample code.

 

        void Main(string[] args)

        {
            String file = "C:\\Program Files\\Autodesk\\AutoCAD 2010\\acad.exe";
            String name = "C:\\documents and Settings\\taox\\Local Settings\\Temp\\_solid_temp.dwg";           
            file = "\"" + file + "\"";
            Console.WriteLine(name);           
            String strInfo = name + " /fi /NoLogo /extapp:ConsoleApplication1";
            ProcessStartInfo startinfo = new ProcessStartInfo(file, strInfo);
            Process pp = Process.Start(startinfo);
            pp.WaitForExit();
        }

你可能感兴趣的:(arguments)