Trying to pass spaces when launching Chrome

爱⌒轻易说出口 提交于 2021-02-18 18:11:01

问题


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"

Here's a snippet of my code:

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

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

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!


回答1:


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

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

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



来源:https://stackoverflow.com/questions/13533301/trying-to-pass-spaces-when-launching-chrome

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!