user34537
user34537

Reputation:

reading while open/writing to file in .net?

I notice many applications like firefox allow me to watch part of a video (using VLC) when the file is still downloading. I would like to do that with my application. When i tried opening the video with VLC i get an error.

How do i allow reading when i write to a file? my open line is

File.Open(fn, FileMode.Append)

i do append so i can resume partial files.

Upvotes: 2

Views: 389

Answers (2)

Hans Passant
Hans Passant

Reputation: 941495

You are leaving it up to the Open() method to pick the FileShare value passed to the FileStream constructor. Which is FileShare.None. Specify your own.

Upvotes: 1

driis
driis

Reputation: 164291

You should use the Open overload, that takes a FileShare parameter:

File.Open(fn, FileMode.Append, FileAccess.Write, FileShare.Read)

This way you explicitly state that other processes are allowed to open the file for reading while you are still writing to it.

Upvotes: 7

Related Questions