启动Chrome时尝试传递空格 [英] Trying to pass spaces when launching Chrome

查看:61
本文介绍了启动Chrome时尝试传递空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从c#Windows窗体启动Chrome的外部实例.只要本地html文件的路径中没有空格,它就可以正常工作.如果有的话,Chrome会在第一个空格处停止.例如:"file:///C:/Users/user/Documents/Visual"我试图通过像Chrome通常那样用%20"替换字符串中的空格来解决此问题.现在我得到这个乱码的地址:"file:///C:/Users/user/Documents/Visual%2520Studio%2520%2012/TEMP.html"

I'm trying to launch an external instance of Chrome from a c# Windows form. It works fine as long as there are no spaces in the path of the local html file. If there are though Chrome stops at the first space. e.g. : "file:///C:/Users/user/Documents/Visual" I tried to fix this by replacing spaces in the string with "%20" like Chrome usually does. Now I get this garbled address: "file:///C:/Users/user/Documents/Visual%2520Studio%2520%2012/TEMP.html"

这是我的代码的片段:

string chromeTempFilePath = tempFilePath.Replace(" ", "%20");

Process.Start(browserPaths[2], chromeTempFilePath);//launch Chrome  

Process.Start 在Firefox和IE 9中都可以正常工作,路径中带有空格.任何帮助将不胜感激,因为我或多或少感到困惑!

Process.Start works fine for both Firefox and IE 9 with the spaces in the path. Any help would be much appreciated as I'm more or less stumped!

推荐答案

删除 string.Replace 方法,并更改您的 Process.Start 如下所示:

Remove the string.Replace method and change your Process.Start to look like this:

Process.Start(browserPaths[2], string.Format("\"{0}\"", chromeTempFilePath));

您只需要将它们全部用双引号引起来即可.这对我来说适用于Chrome浏览器,但是我没有检查其他浏览器.

You just need to wrap it all in double quotes. This worked for me with Chrome, but I didn't check other browsers.

这篇关于启动Chrome时尝试传递空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆