C# open text file read write




















Active 1 year, 11 months ago. Viewed k times. What is the quickest way to read a text file into a string variable? Breeze 1, 2 2 gold badges 31 31 silver badges 39 39 bronze badges. Check this stackoverflow. Add a comment. Active Oldest Votes. KyleMit Not the best function to use, though. As Devendra D. Chavan points out in his answer, StreamReader. ReadToEnd is more efficient. OwenBlacker It depends on whether "quickest" means "least time to execute" or "least time to understand.

ReadAllText is definitively the easiest one to use, but as "Devendra D. Chavan" points out, it is not the fastest. So if you are reading small files, then it would be a better choice to use File.

To read from the server check this , hope helps someone. OwenBlacker -- are you sure? The benchmark shows that StreamReader. Which is to be expected, as the latter also splits the text into lines.

But we are talking about a different method, ReadAllText. ReadToEnd internally. Show 1 more comment. StreamReader approach As the File. Readxxx vs StreamReader. ReadToEnd internally File. InternalReadAllText path, Encoding. Devendra D. Chavan Devendra D. Chavan 8, 4 4 gold badges 29 29 silver badges 33 33 bronze badges. Did you compare against File. ReadAllText , too?? ILSpy suggests that File. ReadAllText is simply a wrapper over StreamReader.

Any additional feedback? Note The files that are used in this example are created in the topic How to write to a text file. Submit and view feedback for This product This page. View all page feedback. In this article. I think the question implies 'at the same time. JohnAtwood You know what they say about assumptions. He never did select a correct answer, so we will never really know. Copperpot: My solution isn't your solution, your problem is different or at a minimum more explained than the OP, however saying my answer will fail is incorrect.

You might be using it wrong, but that's more of an issue with you than with the answer. The memory footprint of this solution can get prohibitively large. Open "file. Close ; The key thing is to open the file with the FileAccess. ScottS ScottS 8, 3 3 gold badges 27 27 silver badges 48 48 bronze badges. This thread seems to answer your question : simultaneous-read-write-a-file Basically, what you need is to declare two FileStream, one for read operations, the other for write operations.

Community Bot 1 1 1 silver badge. S dont forget to add Console. Readline after this code snippet else console will not appear. WriteLine text ; tw.

WriteLine tr. ReadLine ; tr. Close ;. Active Oldest Votes. Open "path to file", FileMode. Open, FileAccess. Read, FileShare. Tom Vervoort Tom Vervoort 4, 2 2 gold badges 20 20 silver badges 19 19 bronze badges. Specifying FileShare. ReadWrite is crucial. The other process has already gained Write access, you cannot deny it.

Thanks, that worked perfectly! No it doesn't. Take a look with reflector: return new FileStream path, FileMode. Read ; — Tom Vervoort. Yes, File. OpenRead did not work for me — Feroc. Gives me the same "file in use" error — MichaelvdNet.



0コメント

  • 1000 / 1000