Solved

FileStream.Write Starting and Stopping

Posted on 2008-10-18
4
778 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
  • 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 85

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

810 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