Unable to close C# files after reading file name into a list box.

Douglas Moncur 0 Reputation points
2024-02-07T13:12:25.54+00:00

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);
    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,343 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.