是否可以在末尾添加选定的文件而不是使用带有多选的 OpenFileDialog 开始?

Can selected files be added at the end rather than beginning using the OpenFileDialog with multiselect?

在启用 multiselect 的情况下使用 OpenFileDialog 时,每次我 select 另一个文件(使用 ctrl 或 shift + 单击)时,最近添加的文件被插入文件的开头名称文本框。有没有办法改变它并让它们添加到最后?

我正在使用 IFileDialog 界面进行一些自定义工作,文件排序对我来说至关重要。

我正在使用 .NET 4.5。

编辑:在做了更多实验之后,我也不确定文件返回后的顺序。它似乎是按字母顺序排列的。谁能证实这一点?我很难为此找到好的 documentation/examples。

如果您想按照单击文件的确切顺序获取所选文件,则不能使用标准的 OpenFileDialog,因为您无法控制返回的文件名的顺序 属性。 相反,您可以轻松地在特定文件夹中构建自己的文件 ListView,并自行跟踪单击的项目的顺序,将它们添加到 List<string>

中并从中删除它们
 List<string> filesSelected = new List<string>();

假设,例如有一个具有这些属性的 ListView

// Set the view to show details.
listView1.View = View.Details;

// Display check boxes.
listView1.CheckBoxes = true;
listView1.FullRowSelect = true;
listView1.MultiSelect = false;

// Set the handler for tracking the check on files and their order
listView1.ItemCheck += onCheck;

// Now extract the files, (path fixed here, but you could add a 
// FolderBrowserDialog to allow changing folders....
DirectoryInfo di = new DirectoryInfo(@"d:\temp");
FileInfo[] entries = di.GetFiles("*.*");

// Fill the listview with files and some of their properties
ListViewItem item = null;
foreach (FileInfo entry in entries)
{
    item = new ListViewItem(new string[] { entry.Name, entry.LastWriteTime.ToString(), entry.Length.ToString()} );
    listView1.Items.Add(item);
}            
listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);                        

// Create columns for the items and subitems.
// Width of -2 indicates auto-size.
listView1.Columns.Add("File name", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Last Write Time2", -2, HorizontalAlignment.Left);
listView1.Columns.Add("Size", -2, HorizontalAlignment.Left);

此时可以使用 onCheck 事件处理程序在跟踪文件列表中添加和删除文件

void onCheck(object sender, ItemCheckEventArgs e)
{
    if (e.Index != -1)
    {
        string file = listView1.Items[e.Index].Text;
        if (filesSelected.Contains(file))
            filesSelected.Remove(file);
        else
            filesSelected.Add(file);
    }
}