Read the actual contents of text file using FileStream and these options c#

Maybe something like: FileStream fileStream = new FileStream(“[path]”, FileMode.Open, FileAccess.Read, FileShare.Read, 64 * 1024, (FileOptions)0x20000000 | FileOptions.WriteThrough & FileOptions.SequentialScan); string fileContents; using (StreamReader reader = new StreamReader(fileStream)) { fileContents = reader.ReadToEnd(); } bool assignedvariable = Convert.ToBoolean(fileContents); assignedvariable will hold true if the file contains 1 and false if it contains 0. Sorry if this has … Read more

Fastest way to read file length C#

The natural way to get the file size in .NET is the FileInfo.Length property you mentioned. I am not sure Stream.Length is slower (it won’t read the whole file anyway), but it’s definitely more natural to use FileInfo instead of a FileStream if you do not plan to read the file. Here’s a small benchmark … Read more

Will a using clause close this stream?

Yes, StreamReader.Dispose closes the underlying stream (for all public ways of creating one). However, there’s a nicer alternative: using (TextReader reader = File.OpenText(“file.txt”)) { } This has the added benefit that it opens the underlying stream with a hint to Windows that you’ll be accessing it sequentially. Here’s a test app which shows the first … Read more

stream.CopyTo – file empty. asp.net

There is nothing wrong with your code. The fact you say “I’ve checked the stream.Length before copying and its not empty” makes me wonder about the stream position before copying. If you’ve already consumed the source stream once then although the stream isn’t zero length, its position may be at the end of the stream … Read more

Saving openpyxl file via text and filestream

In openpyxl 2.6 calling the save_virtual_workbook method issues the following warning: DeprecationWarning: Call to deprecated function save_virtual_workbook (Use a NamedTemporaryFile). At some point save_virtual_workbook will be removed from openpyxl. In Python 3 typical usage to save an openpyxl workbook to a filestream becomes: from io import BytesIO from tempfile import NamedTemporaryFile from openpyxl import Workbook … Read more

FILESTREAM feature is disabled

I was unable to script this, (using the link provided by user1977061), as the MSSQLService requires a restart afterwards to apply the change. Even after doing this it didn’t work, and instead I had to manually configure FILESTREAM usage via: SqlServer Configuration Manager Sql SQL Server Services SQL Server(MSSQLSERVER) [Properties] FILESTREAM tab – enable the … Read more

Easiest way to read text file which is locked by another application

I think you just want the following: using (var fileStream = new FileStream(“foo.bar”, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (var textReader = new StreamReader(fileStream)) { var content = textReader.ReadToEnd(); } The FileAccess.Read parameter is what is important, to indicate that you only want to read the file. Of course, even to do this, the file must have … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)