using System.IO; string filePath = "C:\\Users\\User1\\file.txt"; IFileInfo fileInfo = new FileInfo(filePath); FileStream fileStream = fileInfo.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
using System.IO; string directoryPath = "C:\\Users\\User1\\Documents"; IFileInfo directoryInfo = new DirectoryInfo(directoryPath); FileStream fileStream = directoryInfo.Open(FileMode.OpenOrCreate, FileAccess.Read, FileShare.ReadWrite);In the above example, we are creating an instance of DirectoryInfo class using the directory path "C:\Users\User1\Documents". Then, we are calling the Open method with FileMode.OpenOrCreate, FileAccess.Read, and FileShare.ReadWrite parameters to open a file or create a new file in the directory in read-only mode. Package library: System.IO.FileSystem.