C# deleting an open file - closing all files first?

C# VS2008 Windows app

I check a bunch of text files by checking if they exist and searching for and displaying some data. Later I offer the user a chance to delete all the text files but VS studio has an exception that the file is in use? I didn't know I was leaving the files open - can I issue something to close all files at one?

Karl66
Karl66Asked:
Who is Participating?
 
JimBrandleyConnect With a Mentor Commented:
Issue
sr.Close();
sr.Dispose();

before you try the delete.

Jim
0
 
Karl66Author Commented:
Here is an example -

                  StreamReader sr = File.OpenText(path + @"\tmp\file.txt");
                    while ((input = sr.ReadLine()) != null)
                    {
                          <... bunch of searches ...>
                     }
                     if (Directory.Exists(path + @"\tmp"))
                        System.IO.Directory.Delete(path + @"\tmp", true); <- says cannot delete \tmp in use
0
All Courses

From novice to tech pro — start learning today.