Solved

FileStream.Write Starting and Stopping

Posted on 2008-10-18
4
769 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
Comment Utility
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
Comment Utility
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
Comment Utility
"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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now