Search code examples
c#pdfopenfiledialog

How can I limit an OpenFileDialog to only allow the user to select PDF files?


ofd.Filter = "pdf files (*.pdf)|*.*";

Why is it that it can still brownse non-pdf files? Is something wrong here?


Solution

  • The string is in two parts (FileDialog.Filter docs):

    Label|Extension

    Your label is pdf files (*.pdf), but the extension you are filtering by is *.*. Try setting it to |*.pdf instead.

    In short, the *.pdf you have specified is only a descriptive text. It could be anything. It is not used to filter.