No, this will not close the StreamReader. You need to close it. Using does this for you (and disposes it so it’s GC’d sooner):
using (StreamReader r = new StreamReader("file.txt"))
{
allFileText = r.ReadToEnd();
}
Or alternatively in .Net 2 you can use the new File. static members, then you don’t need to close anything:
variable = File.ReadAllText("file.txt");
Related Contents:
- Should I call Close() or Dispose() for stream objects?
- Does disposing streamreader close the stream?
- How to find and replace text in a file
- Converting file into Base64String and back again
- Reading large text files with streams in C#
- An elegant way to consume (all bytes of a) BinaryReader?
- How to read a large (1 GB) txt file in .NET?
- Memory Leak using StreamReader and XmlSerializer
- StreamReader vs BinaryReader?
- I get a “An attempt was made to load a program with an incorrect format” error on a SQL Server replication project
- How to Convert JSON object to Custom C# object?
- How to delete a file after checking whether it exists
- ASP.NET MVC Razor render without encoding
- Select Tag Helper in ASP.NET Core MVC
- Best way to split string into lines
- Email address validation using ASP.NET MVC data type attributes
- Is floating-point math consistent in C#? Can it be?
- ASP.NET WebApi unit testing with Request.CreateResponse
- How to use System.Net.HttpClient to post a complex type?
- Nullable types: better way to check for null or zero in c#
- LINQ to Entities only supports casting EDM primitive or enumeration types with IEntity interface
- Why can I assign 0.0 to enumeration values, but not 1.0
- How should I remove all elements in a DbSet?
- How does the command prompt know when to wait for exit?
- How to Implement IComparable interface?
- View all TODO items in Visual Studio using GhostDoc
- How to sort depended objects by dependency
- WPF MVVM: How to close a window
- MOQ – setting up a method based on argument values (multiple arguments)
- Creating delegates manually vs using Action/Func delegates
- How to change the button text for ‘Yes’ and ‘No’ buttons in the MessageBox.Show dialog?
- How to scroll to bottom of ListBox?
- Create a comma-separated strings in C#
- Return list using select new in LINQ
- Why is it that “No HTTP resource was found that matches the request URI” here?
- How to suppress InMemoryEventId.TransactionIgnoredWarning when unit testing with in-memory database with transactions?
- How can I get a character in a string by index?
- How to implement and do OCR in a C# project?
- Can I change the default schema name in entity framework 4.3 code-first?
- NotNull attribute
- Why can’t I preallocate a hashset
- How to create a view using EF code-first POCO
- Route parameter with slash “/” in URL
- Expression-bodied function members efficiency and performance in C# 6.0
- How to test asp.net core built-in Ilogger
- C# string format flag or modifier to lowercase param
- Convert string to Time
- “cannot implement interface member” error when interface and concrete are in different projects
- Use “real” CultureInfo.CurrentCulture in WPF Binding, not CultureInfo from IetfLanguageTag
- How to add global `AuthorizeFilter` or `AuthorizeAttribute` in ASP.NET Core?