Reading and writing to files.

Iceache

Member
Joined
Nov 26, 2007
Messages
7
Programming Experience
Beginner
What im trying to do is make a web browser. just for my own personal use..

anyway, what im trying to do is make it so when the program loads, it looks for my homepage file, that if it doesnt exist it creates a blank one, i have the file creation.. now, what I want to do is check if the file is empty, and if its not, read the URL, and then, I want to make a button, that makes the current page the homepage, it will delete the old URL and replace it with the new one.
 
Create a User setting to store the address string. Project properties, Settings tab.
Usage something like:
VB.NET:
browser.navigate(my.settings.homepage)
VB.NET:
my.settings.homepage = current_url
 
that helps.. but what I want to to... is like, on load, check "C:\MyDocuments\WebBrowser\Homepage.pgf" and if its empty, do nothing, but if there is a URL saved, then copy it, and load it into my program.
 
You don't need file to store that string, just use the Setting, it can be empty string too.
 
Independent from what?
 
User settings are stored in their own files. I don't know what you mean by "other browsers".
 
I think he's confusing your hompage' variable name with a property for another webrowser program.

What JohnH's code does is sets the URL for the browser to the String Variable you have saved in the homepage setting of your browser. If you go into the Project Menu at the top, and at the bottom click on Project Properties. Then from there, choose the Settings tab and make a Setting called homepage and then you can call it in your code like above.

JohnH was also saying that it can be empty, so just leave the value space empty and create your button on the form something like this:

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim currentURL As String
        Me.textbox1.text = currentURL

        My.Settings.homepage = currentURL

Where textbox1 is your Address Bar you are using for your browser.
 
yes but that doesnt save after I close my program

what I want to do, is load the homepage everytime i open the browser.. I kinda have it working.. I have it save to a text file.. but is there a way to erase the text in the file before it writes text to it so that way it overwrites the text each time?
 
very, but anyway.. that doesnt matter..

now what im trying to do is this.

Dim TargetFile As StreamWriter

Try
TargetFile = New StreamWriter(Environ$("UserProfile") & "\My Documents\WebBrowser\Homepage.pgf", True)
Catch
MessageBox.Show("Error opening " & Environ$("UserProfile") & "\My Documents\WebBrowser\Homepage.pgf")
End Try

Try


Catch
Dim d As New StreamReader(fs)
d.ReadToEnd()
TargetFile.Write(0)

End Try

TargetFile.Close()
MessageBox.Show("Text saved to " & Environ$("UserProfile") & "\My Documents\WebBrowser\Homepage.pgf")

what that does, is it writes whatever is in the URL box into the file.. but, what I want it to do, is overwrite instead of append to the end.
 
You have to be sure that the check box that says "save settings on exit" is checked. It's defultly set to do so like JohnH said, but maybe you accidently unchecked it or something.
 
Change to this:

TargetFile = New StreamWriter(Environ$("UserProfile") & "\My Documents\WebBrowser\Homepage.pgf", False)
The file will be overwritten
 
Back
Top