Solved

FileStream.Write Starting and Stopping

Posted on 2008-10-18
4
785 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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

732 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