OpenFileDialog C#:如何按照用户选择的顺序加载文件

问题描述 投票:0回答:1

我已将

Multiselect 
设置为 true,以便能够一次加载多个文件。问题是它忽略了用户选择文件的顺序,文件名列表始终相同(如果我以不同的顺序选择同一组文件)。

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

有没有办法让文件按照用户选择的顺序排列?

c# winforms openfiledialog
1个回答
0
投票

尝试这样写:

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
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.