Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to access a html file placed in html folder at root directory as a url for webbrowser control in winform application
Posted

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:
C#
string exeDir = System.IO.Path.GetDirectoryName(
   System.Reflection.Assembly.GetEntryAssembly().Location;


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
 
Share this answer
 
Comments
Sandeep Mewara 6-May-11 10:45am    
5++!
Sergey Alexandrovich Kryukov 6-May-11 10:55am    
Thank you, Sandeep.
--SA
IncredibleRam 6-May-11 11:14am    
ok i didn't read the Question Carefully sorry
5++
Sergey Alexandrovich Kryukov 6-May-11 11:33am    
Thank you very much.
No problem, it happens.
--SA
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 code :(Converted by a tool from c# to VB):
webBrowser.Url = New Uri("www.abc.com\Html\Html1.html")


also here webBrowser is the name of the WEBBROWSER control.
 
Share this answer
 
v2
Comments
Yatin Bhagat 7-May-11 1:20am    
actully i am not reffring any web site,i have designed my own html file and placed it in root directory,in this what should be the steps???

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900