hi,
i need to read a file that is already opened exclusively by another
process. yes this seems like a contradiction, but i can always do
File.Copy on that file, regardless of how 'locked' it is. and yet i
cannot use File.Open() with FileAccess.Read.
this behaviour is widely reported on the newsgroups regarding .Net 1.1,
but it seems to be the same in .net 2.0. i have not found any
explanations of it. surely File.Copy internally must open a file
stream on the locked file and just stream the bytes out to the
destination file, so i'm baffled as to what the difference is with
File.Open(...read...)
windows behaves the same way with locked files. if there is a file you
cannot open, you can still copy it. i need to do this in .net. volume
shadow copy is not an option. also, i don't want to use File.Copy to a
temp location, then read it, and then delete it. that is just plain
wrong.
this is the code i'm using:
FileStream stream = File.Open(filename, FileMode.Open,
FileAccess.Read);
does anyone have ideas?
thanks
tim
Willy Denoyette [MVP] - 28 Jan 2006 23:59 GMT
| hi,
| i need to read a file that is already opened exclusively by another
[quoted text clipped - 21 lines]
| thanks
| tim
You are trying to open the file in non shared mode, this isn't possible if
the file is already open right?
Use the File.Open overload that takes four arguments and specify
FileShare.ReadWrite as sharing mode.
Willy.
Tim_Mac - 29 Jan 2006 09:45 GMT
hi willy, you're right! that does it. i always thought that the
FileShare options only affected other processes opening the file after
i had opened it, not existing file-sharing conditions.
that's a great help, thank you so much.
tim