[英]C# webbrowser navite to local file. Spaces in path
I'm using webbrowser control in C# application to display different local files, not necessarily web pages but also images and pdf.files. 我在C#应用程序中使用webbrowser控件来显示不同的本地文件,不一定显示网页,还显示图像和pdf.files。
I noticed that it works: 我注意到它有效:
string path = "C:\MyDirectory\file.png";
webBrowser1.Navigate(path,false);
but if there are spaces in directories names it fails: 但是如果目录名称中有空格,它将失败:
string path = "C:\My Directory\file.png";
webBrowser1.Navigate(path,false);
What is the trick to display such files correctly in webbrowser control? 在webbrowser控件中正确显示此类文件的诀窍是什么?
Prefix your string with @ for local and UNC paths. 对于本地和UNC路径,请使用@前缀字符串。
string localpath = @"drive:\Some Folder\file with spaces.extension";
string uncPath = @"\\network machine\Resources\Some Resource.extension";
Hi sorry I can't post a comment without more rep but if I understand correctly, the WebBrowser class is basically like a IE wrapper, and so URLs with spaces or special characters in need to be encoded like so: 嗨,抱歉,如果没有更多代表,我将不会发表评论,但是如果我理解正确,WebBrowser类基本上就像是IE包装器,因此需要对带有空格或特殊字符的URL进行编码,如下所示:
string path = System.Uri.EscapeDataString(url)
So that it will replace spaces with %20 etc and WebBrowser should know where to go. 这样它将用%20等代替空格,并且WebBrowser应该知道要去哪里。
Checkout the docs for EscapeDataString 签出EscapeDataString的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.