IO Exception

IO Exception

sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
Okay, so I am making a web browser.
I have a StreamWriter that writes the current url to a file. I also I have a StreamReader that (on form load) reads the file and populates my combobox with the urls. The problem is if I put the StreamWriter in the go button click method it will close and I will only write 1 line (if there are multiple urls it only writes the last one -well actually it writes all the urls the problem is it writes over the previous url). If I put the StreamWriter in the declarations I get a system IO Exception, because my StreamReader already has access the the file (the StreamWriter successfully writes multiple urls if I disable the StreamReader). So I need to find either a place for the StreamReader or StreamWriter...

Wait....

Fuck yes! My first use of an array that worked!

C# code
//Way up top ArrayList urllist = new ArrayList(); private void Form1_FormClosing(object sender, FormClosingEventArgs e) { StreamWriter sw = new StreamWriter("history.txt"); foreach (string temp in urllist) { sw.WriteLine(temp); } sw.Close(); sw.Dispose(); }
 
 
 
2010 Jan 28 at 06:16 UTC — Ed. 2010 Jan 28 at 06:39 UTC
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
The problem with this code, is each time you open the application is erases the history of the previous use.
 
 
 
2010 Jan 28 at 20:42 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
There are a few ways you can open a file for writing. One is 'append', where the data is stuck on the end. Rather than the current erase and start over thingy.
 
 
 
2010 Jan 29 at 00:23 UTC
sprinkles

Chrome Whore
2009 Sep 6 • 2547
10 ₧
I don't believe StreamWriter has an append method, but I also have to check if there is the same url already in the file. No it doesn't, but:


code
using (StreamWriter sw = File.AppendText(path)) { sw.WriteLine("This"); sw.WriteLine("is Extra"); sw.WriteLine("Text"); }
 
 
 
2010 Jan 31 at 04:42 UTC — Ed. 2010 Jan 31 at 04:47 UTC
Page [1]