2

In my c# windows application, I like to get multiple files with a same order as user select the file in openfiledialog window. I'm getting all the selected file but its order is not correct. Anyone help me to solve this problem.

4
  • 1
    You should create your own dialog Commented Jul 12, 2016 at 4:17
  • What kind of order ? Just sort it with your own method openFileDialog.FileNames, probably that is easier. Commented Jul 12, 2016 at 4:20
  • If user select first file it should be on index [0] then if user selects second file should be on index[1] ...so on. While assigning the openFileDialog.FileNames to a string array the above order should meet. Commented Jul 12, 2016 at 4:28
  • @dotctor can u suggest me any links or give me a code for creating own dialog that meet my constraint Commented Jul 12, 2016 at 11:25

1 Answer 1

5

If you look at the documentation for the underlying control's interface OPENFILENAME, you'll see this comment:

If the user selects more than one file, the lpstrFile buffer returns the path to the current directory followed by the file names of the selected files.

And, other than some comments on the separator characters used for different scenarios, what to do if there's not enough buffer, and some details on legacy 8.3 support, that's it. In other words, the OpenFileDialog doesn't offer any mechanism to bubble up the sort information as the underlying Windows Common Dialog Box doesn't offer anyway to get this information out via a standard interface.

If you must preserve it, you'll need to create your own Form or Dialog to track the selection in order, and honestly you'll probably be better off providing an slightly different UI that allows the user to reorder files after they've selected them, similar to how one can reorder lists on some mobile devices.

Sign up to request clarification or add additional context in comments.

2 Comments

It would be even more useful to have some documentation or source confirming the statement, and perfect if we could know how the OS actually fill the list of selected files. I mean, it definitely must not be using Random() to create the list, right? lol
It's challenging to link to proof of this, as the underlying documentation for either the .NET control or the underlying Win32 API makes no comment about returned order, suggesting while there is probably an order, it's at the whim of the implementation and probably changes over time. In other words, it's undocumented behavior. That said, I've added a link to the documentation, and some details on the missing guarantees.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.