What do you usually use to open a file?
Using (filestream FS = new filestream (file, filemode. Open )?
Using (filestream FS = new filestream (file, filemode. Open, fileaccess. Read )?
Using (filestream FS = new filestream (file, filemode. Open, fileaccess. Read, fileshare. readwrite ))?
The constructors of. Net System. Io. filestream include:
public FileStream(string path, FileMode mode) : this(path, mode, (mode == FileMode.Append) ? FileAccess.Write : FileAccess.ReadWrite, FileShare.Read, 0x1000, |
The default value of fileshare is fileshare. Read.
If we open a file with fileshare. Read, and the file has been opened by another filestream before this, and there is no close (), a system. Io. ioexception will be generated.
Therefore, if you only want to read files, use using (filestream FS = new filestream (file, filemode. Open, fileaccess. Read, fileshare. readwrite )).
Is that all right?
Because fileshare. None and fileshare. Delete exist, the answer is no.
If the file is opened in fileshare. None mode, throw exception, even if only read. Fortunately, this case is infrequence.