Do I need to explicitly close the StreamReader in C# when using it to load a file into a string variable?

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: … Read more

Memory Leak using StreamReader and XmlSerializer

The leak is here: new XmlSerializer(typeof(XMLObj), new XmlRootAttribute(“rootNode”)) XmlSerializer uses assembly generation, and assemblies cannot be collected. It does some automatic cache/reuse for the simplest constructor scenarios (new XmlSerializer(Type), etc), but not for this scenario. Consequently, you should cache it manually: static readonly XmlSerializer mySerializer = new XmlSerializer(typeof(XMLObj), new XmlRootAttribute(“rootNode”)) and use the cached serializer … Read more

How do I open an already opened file with a .net StreamReader?

As Jared says, You cannot do this unless the other entity which has the file open allows for shared reads. Excel allows shared reads, even for files it has open for writing. Therefore, you must open the filestream with the FileShare.ReadWrite parameter. The FileShare param is often misunderstood. It indicates what other openers of the … Read more

An elegant way to consume (all bytes of a) BinaryReader?

Original Answer (Read Update Below!) Simply do: byte[] allData = read1.ReadBytes(int.MaxValue); The documentation says that it will read all bytes until the end of the stream is reached. Update Although this seems elegant, and the documentation seems to indicate that this would work, the actual implementation (checked in .NET 2, 3.5, and 4) allocates a … Read more

How do you send an HTTP Get Web Request in Python? [duplicate]

You can use urllib2 import urllib2 content = urllib2.urlopen(some_url).read() print content Also you can use httplib import httplib conn = httplib.HTTPConnection(“www.python.org”) conn.request(“HEAD”,”/index.html”) res = conn.getresponse() print res.status, res.reason # Result: 200 OK or the requests library import requests r = requests.get(‘https://api.github.com/user’, auth=(‘user’, ‘pass’)) r.status_code # Result: 200

Reading large text files with streams in C#

You can improve read speed by using a BufferedStream, like this: using (FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) using (BufferedStream bs = new BufferedStream(fs)) using (StreamReader sr = new StreamReader(bs)) { string line; while ((line = sr.ReadLine()) != null) { } } March 2013 UPDATE I recently wrote code for reading and processing (searching … Read more

Converting file into Base64String and back again

If you want for some reason to convert your file to base-64 string. Like if you want to pass it via internet, etc… you can do this Byte[] bytes = File.ReadAllBytes(“path”); String file = Convert.ToBase64String(bytes); And correspondingly, read back to file: Byte[] bytes = Convert.FromBase64String(b64Str); File.WriteAllBytes(path, bytes);