Get to know fileshare

Source: Internet
Author: User
Tags throw exception

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.