OpenFileDialog C#: How to load files in the order the user selected them

111 Views Asked by At

I have set Multiselect to true in order to be able to load several files at once. The problem is that it ignores the order in which the user selects the files, the list of FileNames is always the same (if I select the same set of files in different order).

 if (openFileDialog1.ShowDialog() == DialogResult.OK)
 {
     for (int i = 0; i < openFileDialog1.FileNames.Length; i++)
     {
         string file = openFileDialog1.FileNames[i];
         
     }
 }

Is there a way to let the files be in the order the user chooses?

1

There are 1 best solutions below

1
Nguyễn Đức Kiên On

try writing it like this:

if (openFileDialog1.ShowDialog() == DialogResult.OK) {
  List<string> selectedFiles = openFileDialog1.FileNames.ToList();
  selectedFiles.Sort(); // Sort the list by file name order

  foreach (string file in selectedFiles) {

    // Handle files here, they will be processed in the order the user selected
  }
}