Unable to close C# files after reading file name into a list box.
Douglas Moncur
0
Reputation points
After executing this code and the file names are in the list box, If I select a file to delete programmatically I get the file in use by another process. NOTE: I have not included any exception processing yet, I just want it to read the list of files in a directory, display them in a list box, and when a user chooses to delete a file, I want to delete that file. This listbox gets loaded ok.
public int GetFolderContents()
{
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
var fldrStatus = folderBrowserDialog.ShowDialog();
if (fldrStatus == DialogResult.OK)
{
selectedFolder = folderBrowserDialog.SelectedPath;
//folderBrowserDialog.Dispose();
DirectoryInfo mydir = new DirectoryInfo(selectedFolder);
var listOfFileNames = mydir.GetFiles();
////var files = from file in Directory.EnumerateFiles(selectedFolder) select file;
string tempString = string.Empty;
foreach (FileInfo file in listOfFileNames)
{
tempString = selectedFolder + "\\" + file.Name;
myListBox.Items.Add(tempString);
}
return 0;
}
return -1;
}
Here is the delete file method.
public void DeleteTheFIle(int indexOfItemToBeDeleted, string fileToDelete)
{
// TODO For debug breakpoint only
int x = 0;
///string temp = @"C:\MediaMgrPictures\Default-4.jpg";
////myListBox.Items.RemoveAt(indexOfItemToBeDeleted);
if(System.IO.File.Exists(fileToDelete))
{
MessageBox.Show("File is there!!!");
//System.IO.File.Delete(temp);
System.IO.File.Delete(fileToDelete);
}
}
Sign in to answer