From my understanding, ReadAllLines would open a file, and then return all the lines within that file, and then closes that file/stream. Now I have this piece of code:
try
{
string[] lines = File.ReadAllLines(path);
}
catch(IOException)
{
Console.WriteLine("File doesnt exist in : " + path);
}
I did that so that if the file within the path directory doesnt exist, it would throw an error message. My question: is that necessary? Since I dont know how ReadAllLines() was implemented by microsoft, I dont really know if it already had a try catch built-in within the function implementation..
I however, can “guess” that ReadAllLines() would always close the file stream everytime it finishes reading it. that is the reason I did not run the cleanup code that is supposed to be included within the finally{} block.
Can somebody explain/give me confirmation about this? Any help would be appreciated. Let me know if the question is not clear. Thank you.
I looked at the source in ILSpy and it looks like it does the following:
And the method InternalReadAllLines: