-
September 20th, 2010, 01:18 AM
#1
Textwriter
As some of you might already know from my previous topic, I am trying to write the text in a combobox to a text file.
After some help, I finally got a code that compiled. The problem is, now it's álways telling me the file is in use. So I was wondering if anyone could take a look at my code, because I'm using the 'using' statement, and cant find any other open stream. Maybe there's something I'm missing:
Code:
string bank = lFileName.Text.ToString() + ".txt";
string banknr = lFileName.Text.ToString() + "nr.txt";
string bankvar = lFileName.Text.ToString() + "VAR.txt";
using (System.IO.TextWriter wr = System.IO.File.CreateText(bank))
{
File.WriteAllText(bank, cbBanken.Items.ToString());
}
using (System.IO.TextWriter wr = System.IO.File.CreateText(banknr))
{
File.WriteAllText(banknr, cbBanken.Items.ToString());
}
using (System.IO.TextWriter wr = System.IO.File.CreateText(bankvar))
{
File.WriteAllText(bankvar, cbBanken.Items.ToString());
}
It's kind of a weird case, because after I get the "The file is already open" -error, I can still save to that file, I just cant write anything.
I tried a couple of things already, the closest I ever got is that it emptied out my file. :P
Thanks in advance.
Last edited by Choseal; September 20th, 2010 at 01:21 AM.
-
September 20th, 2010, 03:06 AM
#2
Re: Textwriter
Originally Posted by Choseal
Code:
using (System.IO.TextWriter wr = System.IO.File.CreateText(bank))
{
File.WriteAllText(bank, cbBanken.Items.ToString());
}
You create a TextWriter object, but you don't use it.
When you use the File.WriteAllText method, the TextWriter isn't even necessary. Just use
Code:
File.WriteAllText(bank, cbBanken.Items.ToString());
-
September 20th, 2010, 03:13 AM
#3
Re: Textwriter
I managed to fix it, but turns out you cant edit a combobox and thén save the content. It will just revert back to the original Combobox content.
Last edited by Choseal; September 20th, 2010 at 04:41 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|