

read all texts using StreamReader object string fileContent = sr.ReadToEnd() get the StreamReader StreamReader sr = new StreamReader(fsToRead) open DummyFile.txt for write operation FileStream fsToWrite = fi.Open( FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite) If you are already reading from a file, create a separate FileStream object to write to the same file, as shown below: //Create FileInfo object for DummyFile.txt FileInfo fi = new FileInfo( DummyFile.txt for read operation FileStream fsToRead = fi.Open( FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite) Read and Write operations are not possible on the same FileStream object simultaneously. Sw.WriteLine( "Another line from streamwriter")

Create StreamWriter object to write string to FileSream StreamWriter sw = new StreamWriter(fs) The following example shows how StreamWriter makes it easy to write strings to a File:Įxample: Write texts to file using StreamWriter //Create object of FileInfo for specified path FileInfo fi = new FileInfo( file for Read\Write FileStream fs = fi.Open( FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read )

Notice that fi.Open() has three parameters: The first parameter is FileMode for creating and opening a file if it does not exist the second parameter, FileAccess, is to indicate a Read operation and the third parameter is to share the file for reading with other users while the file is open. Close StreamReader object after operation Use ReadToEnd method to read all the content from file string fileContent = sr.ReadToEnd() Create object of StreamReader by passing FileStream object on which it needs to operates on StreamReader sr = new StreamReader(fs) The following example shows how StreamReader makes it easy to read strings from a file: //Create object of FileInfo for specified path FileInfo fi = new FileInfo( file for Read\Write FileStream fs = fi.Open( FileMode.OpenOrCreate, FileAccess.Read, FileShare.Read) The same read/write operation can be done easily using StreamReader and StreamWriter.
FILESHARE READWRITE CODE
Once you read all the bytes from FileStream, you can convert it into string using UTF8 encoding string filestring = (fileBytes) Īs you have seen in the above code, you have to write lot of code for reading/writing a string from a FileSream. Int n = fs.Read(fileBytes, numBytesRead, numBytesToRead) iterate till all the bytes read from FileStream while (numBytesToRead > 0) Counter to indicate number of bytes already read int numBytesRead = 0 Decrease the counter as you read each byte int numBytesToRead = ( int)fileBytes.Length define counter to check how much butes to read. create byte array of same size as FileStream length byte fileBytes = new byte
FILESHARE READWRITE HOW TO
The following example shows how to read bytes from a file manually and then convert them to a string using UTF8 encoding: //Create object of FileInfo for specified path FileInfo fi = new FileInfo( file for Read\Write FileStream fs = fi.Open( FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite) Replaces the contents of a specified file with the file described by the current FileInfo object, deleting the original file, and creating a backup of the replaced file. Moves a specified file to a new location, providing the option to specify a new file name.Ĭreates a StreamReader with UTF8 encoding that reads from an existing text file. Gets a FileSecurity object that encapsulates the access control list (ACL) entries for a specified file. Gets the size, in bytes, of the current file.Ĭreates a StreamWriter that appends text to the file represented by this instance of the FileInfo.Ĭopies an existing file to a new file, disallowing the overwriting of an existing file.Ĭreates a StreamWriter that writes a new text file.ĭecrypts a file that was encrypted by the current account using the Encrypt method.Įncrypts a file so that only the account used to encrypt the file can decrypt it. Gets or sets the time when the current file or directory was last written to

Gets or sets the time the current file or directory was last accessed Gets or sets a value that determines if the current file is read only.
FILESHARE READWRITE FULL
Gets the full path of the directory or file. Gets the string representing the extension part of the file. Gets a value indicating whether a file exists. Gets a string representing the directory's full path. Gets an instance of the parent directory. Important properties and methods of FileInfo: Property The FileInfo class provides the same functionality as the static File class but you have more control on read/write operations on files by writing code manually for reading or writing bytes from a file. Here, we will use FileInfo class to perform read/write operation on physical files. You have learned how to perform different tasks on physical files using static File class in the previous section.
