dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Textwriter

  1. #1
    Join Date
    Sep 2010
    Posts
    7

    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.

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Textwriter

    Quote Originally Posted by Choseal View Post
    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());

  3. #3
    Join Date
    Sep 2010
    Posts
    7

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)