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

本文探讨了在Windows XP和Windows 7上通过命令行启动应用程序时遇到的问题:含有空格的文件路径作为参数传递时,在XP系统中无法正确解析,导致指定文件无法打开。文章提供了通过引用括起来参数的方法来确保跨不同操作系统的一致性。

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();
        }

转载于:https://www.cnblogs.com/taoxu0903/archive/2010/10/12/1848567.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值