如果您想按照您點擊它們的確切順序來獲取所選文件,則無法使用標準OpenFileDialog,因爲您無法控制返回的FileNames屬性的順序。 相反,你可以輕鬆地建立自己的文件的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);
}
}
那麼反轉Filenames數組呢? – Steve
我實際上試圖改變它們在對話框仍然打開時顯示的順序(在用戶選擇OK之前)。在我的問題中,我可能沒有說清楚。 –
不,它很明顯,但不清楚(並觸發了我的答案)是原因。我認爲重要的是你在選擇之後執行的代碼,而不是它們如何顯示。在任何情況下,我認爲他們選擇以這種方式添加它們以便於在添加許多文件時看到最後一個文件,並且其中一些文件從文本框滾動。 – Steve