Solved

FileStream.Write Starting and Stopping

Posted on 2008-10-18
4
788 Views
Last Modified: 2012-05-05
Is the below pseudocode possible in C#?  If so, could someone please provide a short way or explanation of how to do it?

Start FileStream.Write
Stop FileStream.Write
<Do some stuff>
Resume FileStream.Write where it left off

0
Comment
Question by:jason4659
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Expert Comment

by:drichards
ID: 22748924
You can do it, but how you do it depends on what you mean by "Stop FileStream.Write".  If you keep a reference to the stream around, you can keep writing as long as you haven't closed it.  If you close the stream, you need to reopen it with mode Append, and then you can keep writing to the same file.
0
 

Author Comment

by:jason4659
ID: 22748983
Sorry, I should not have said resume where I left off.  I actually need to skip a part of the file that I'm writing to and resume from there.  So I suppose it would be more like:

Start FileStream.Write
Stop FileStream.Write
Skip Ahead in File
Resume FileStream.Write
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22749253
"Skip Ahead in File"

Use the FileStream.Seek() method to change the current position in the file:
http://msdn.microsoft.com/en-us/library/system.io.filestream.seek.aspx
0
 
LVL 19

Expert Comment

by:drichards
ID: 22754324
A couple of things to note when using seek:

When you start writing to a position in the middle of the file, it overwrites the content from that point forward.  If you want to insert, you need to first copy the data past the seek point and rewrite it after the new data.

If you write data into the middle of the file and do not pass the previous end of the file, the old data will still be there at the end.  You can use SetLength to truncate the file to the end of the desired data.
http://msdn.microsoft.com/en-us/library/system.io.filestream.setlength.aspx

One question, though.  If you start and stop writing a file, what is there to skip ahead to?  Seems like you'd be operating on an existing file for this scenario.  Or are you seeking back to the beginning at some point?

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question