Jump to content
  • Advertisement
Sign in to follow this  
Flopid

[.net] How to correctly read and append to files in C#?

This topic is 3573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to create a file and read from it in the same program (like a word processor proggy and without arrays) in C#. However I get exceptions saying that the file is already used by another proccess, when I try to write to it after I have already read it. I guess I just need the correct way to close the file so I can edit it or read it without exiting the program. Also if I close the file by calling the Flush() and Close() methods it throws an exception saying that you can't access a closed file, when I try to write to it again without exiting. Looked all over google but still can't figure this out. Thanks

Share this post


Link to post
Share on other sites
Advertisement
The using-statement guarantees that the file is closed no matter what happens, even if an exception occurs:

using(var Stream = new System.IO.FileStream("File.ext", System.IO.FileMode.Create, System.IO.FileAccess.Write))
{
//TODO: Write stuff
}

using(var Stream = new System.IO.FileStream("File.ext", System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
//TODO: Read stuff
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Flopid
I am trying to create a file and read from it in the same program (like a word processor proggy and without arrays) in C#. However I get exceptions saying that the file is already used by another proccess, when I try to write to it after I have already read it.


How are you opening the file (StreamReader, FileOpen, etc.)? Let's see your open code block. It sounds like you're not setting the correct FileMode or FileAccess when opening. Just so I'm reading this right, you want to open a file, read from it and write to it in the same application? Does it contain text data or binary data?

Share this post


Link to post
Share on other sites
I am using the file stream to write to the file. By adding the using statements I was able to acomplish what I needed since it looks like they dispose of the file each time a method is run. I have another question though. Is it possible to use something like IDispose on the file stream and not use the using statements?

Share this post


Link to post
Share on other sites
You can call Dispose() manually, but you shouldn't unless you need the object to live longer than you can write a using-block for. using-blocks are safer. There is no way to make the Dispose() call more automatic than a using-block while at the same time maintaining deterministic invokation of said Dispose() call.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!