1

Possible Duplicate:
C#: Is there a way to check if a file is in use?

I created an app to learn c#. All it does is list the temp directory in a listview and then you click a button to delete the files in that dir but when i do some of the files are in use how can i sift through the files skipping the ones in use while deleting the ones that are not.

Community
  • 1
  • 1
partialdata
  • 93
  • 4
  • 14

1 Answers1

1

Trying to predict the behavior of the file system is a futile effort as any other process can change state out from under you. The best way to proceed is to just try your operation and catch the exception that can result from it's failure

foreach (var name in Directory.GetFiles(somePath)) {
  try {
    File.Delete(name);
  } catch (Exception) {
    // Ignore the failure and continue
  }
}
JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
  • ahh ok that makes sense. here is what my deletion code looks like as of now code try { Array.ForEach(Directory.GetFiles(tFile), delegate(string path) { File.Delete(path); }); } catch (IOException err) { MessageBox.Show(err.ToString()); } code – partialdata Apr 01 '11 at 18:41