如何从根目录的html文件夹访问文件 [英] How to access a file from html folder of root directoty

查看:136
本文介绍了如何从根目录的html文件夹访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在winform应用程序中访问放置在根目录html文件夹中的html文件作为webbrowser控件的URL.

How to access a html file placed in html folder at root directory as a url for webbrowser control in winform application

推荐答案

使用此文件的全名. :-)

这很丑,根本行不通.如果在某些系统上为C:/myHtmlFile.html,则不存在"C:"(信不信由你)之类的东西.在Windows 7上,您将文件放入此目录时会遇到麻烦. 您实际上绝对不应在任何应用程序中使用硬编码绝对路径. 您也不能对工作目录进行任何假设:这取决于用户如何启动应用程序,并且可以每次都不同.

该路径应始终在运行时计算.如果在运行时未修改文件,则可以放入可执行目录或相对于此目录的相对路径.这是找到它的方法:
By a full name of this file. :-)

This is ugly and simply won''t work. If this is some C:/myHtmlFile.html on some systems there is no such thing as "C:" (believe or not). On Windows 7 you will have trouble putting the file in this directory. You should really never use hard-code absolute path in any applications. You cannot also make any assumption about the working directory: it depends on how the user starts the application and can be different every time.

The path should always be calculated during run-time. If the file is not modified during run-time, you can put in in the executable directory or a relative path relative to this directory. This is how to find it:
string exeDir = System.IO.Path.GetDirectoryName(
   System.Reflection.Assembly.GetEntryAssembly().Location;



警告!可以通过不同的方式进行操作-它们的通用性较低,并且可能取决于程序集的托管方式或其他程序集的位置.此方法始终返回应用程序入口程序集的主要可执行模块的位置(通常是EXE文件).

现在,如果您的文件是读写文件,则应使用特殊路径访问文件. System.Environment.GetFolderPath(System.Environment.SpecialFolder)(通常与System.Environment.SpecialFolder.LocalApplicationData一起使用).
请参阅 http://msdn.microsoft.com/en-us/library/system.environment .specialfolder.aspx [ ^ ], http://msdn.microsoft.com/en-us/library/14tx8hby .aspx [^ ] .

-SA



Warning! There are different ways to do that — they are less universal and may depends on how the assembly is hosted or where the other assemblies are. This method always returns the location of main executable module of the entry assembly of your application (normally, this is EXE file).

Now, if your file is read-write, you should use a special path where your files can be accessed. Use System.Environment.GetFolderPath(System.Environment.SpecialFolder), typically with System.Environment.SpecialFolder.LocalApplicationData.
See http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx[^], http://msdn.microsoft.com/en-us/library/14tx8hby.aspx[^].

—SA


webBrowser.Url =新Uri(@网站的URL \文件夹名称\ Page.html的名称");

还可以在根目录中的Html页面的此处使用绝对URl.
好像该站点的名称是www.abc.com,则该文件夹的名称是Html,该页面的名称是Html1,扩展名为.html:

C#代码:
webBrowser.Url = new Uri(@"URL for the Website\Name of the Folder\Name of the Page.html");

also use the absolute URl here of the Html page which is in the root directory.
as if the name of the site is www.abc.com then and the name of the folder is Html and the name of the Page is Html1 with the extension .html :

C# Code :
webBrowser.Url = new Uri(@"www.abc.com\Html\Html1.html");



vb.net代码:(由工具从c#转换为VB):



vb.net code :(Converted by a tool from c# to VB):

webBrowser.Url = New Uri("www.abc.com\Html\Html1.html")



同样在这里,webBrowser是WEBBROWSER控件的名称.



also here webBrowser is the name of the WEBBROWSER control.


这篇关于如何从根目录的html文件夹访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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