PrepAway - Latest Free Exam Questions & Answers

Which code segment should you use?

You are developing an application. The application includes a method named ReadFile that reads
data from a file.
The ReadFile() method must meet the following requirements:
It must not make changes to the data file.
It must allow other processes to access the data file.
It must not throw an exception if the application attempts to open a data file that does not exist.
You need to implement the ReadFile() method.
Which code segment should you use?

PrepAway - Latest Free Exam Questions & Answers

A.
var fs = File.Open(Filename, FileMode.OpenOrCreate, FileAccess.Read,
FileShare.ReadWrite);

B.
var fs = File.Open(Filename, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);

C.
var fs = File.Open(Filename, FileMode.OpenOrCreate, FileAccess.Read,
FileShare.Write);

D.
var fs = File.ReadAllLines(Filename);

E.
var fs = File.ReadAllBytes(Filename);

Explanation:
FileMode.OpenOrCreate – Specifies that the operating system should open a file if it exists;
otherwise, a new file should be created. If the file is opened with FileAccess.Read,
FileIOPermissionAccess.Read permission is required. If the file access is FileAccess.Write,
FileIOPermissionAccess.Write permission is required. If the file is opened with FileAccess.ReadWrite,
both FileIOPermissionAccess.Read and FileIOPermissionAccess.Write permissions are required.
http://msdn.microsoft.com/en-us/library/system.io.filemode.aspx
FileShare.ReadWrite – Allows subsequent opening of the file for reading or writing.If this flag is not
specified, any request to open the file for reading or writing (by this process or another process) will
fail until the file is closed.However, even if this flag is specified, additional permissions might still be
needed to access the file.
http://msdn.microsoft.com/pl-pl/library/system.io.fileshare.aspx

2 Comments on “Which code segment should you use?


Leave a Reply