C# FileStream read text with StreamReader using FileStream fs = File. OpenRead(fileName); using var sr = new StreamReader(fs); We pass the FileStream to the StreamReader . If we do not explicitly specify the encoding, the default UTF8 is used.
The FileStream is a class used for reading and writing files in C#. It is part of the System.IO namespace. To manipulate files using FileStream, you need to create an object of FileStream class. This object has four parameters; the Name of the File, FileMode, FileAccess, and FileShare.
FileStream buffers input and output for better performance. This type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try / catch block.
The FileStream's Name property.
See documentation in MSDN
You can use fs.Name to get the path.
Use FileInfo-Class for getting the path.
var fileStream = File.OpenRead(fileName);
var fileInfo = new FileInfo(fileName);
Settings.Default.ThePath = fileInfo.DirectoryName;
Settings.Default.Save();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With