I need to read the string in the text file which contains the default image name and then close the ReadLine(). The way I am trying to close it now is no good and the process is not terminated and
causes “file being used by another process” errors elsewhere in the program, when the text file in question needs to be overwritten.
DirectoryInfo sourceDir = new DirectoryInfo(System.Web.HttpContext.Current.Request.MapPath("~/Content/ProductImages/" + Model.Products[i].ProductID.ToString() + "/thumbs/"));
FileInfo[] defaultImage = sourceDir.GetFiles("defaultImage.txt");
string defimg = defaultImage[0].OpenText().ReadLine();
defaultImage[0].OpenText().Close();
You close the stream that you opened, instead of opening another stream and close that instead…
Or using a
usingblock: