Is it possible to use OpenFileDialog in XNA 4.0 to get the file path? If not, what is the alternative? I'm using XNA 4.0 to create a graphic display (not a game) and I have a file that contains informations on how and what to draw on screen. The only problem is that I hard-coded the path because I don't know how to dynamically get that path? Do you know a solution for that?

  • 2
    \$\begingroup\$ Have you seen the XNA with WinForms examples on the App Hub? Links are in this answer: stackoverflow.com/a/6558501/593627 \$\endgroup\$ Commented Apr 27, 2012 at 10:15
  • \$\begingroup\$ No I haven't, thanks for the link, I'll take a look when I get the time. \$\endgroup\$
    – NDraskovic
    Commented Apr 27, 2012 at 10:19
  • \$\begingroup\$ You can put the information in an XML file, have it build as content and then load it in game the same you load textures and models. Only problem is that you can't change it then without rebuilding. You can also give it the path relative from your .exe (on PC) using a new Uri(...) with the Relative option set. \$\endgroup\$
    – Roy T.
    Commented Apr 27, 2012 at 10:26
  • \$\begingroup\$ I know about the XML approach but I can't use it because the application will be used on multiple computers (that is the root of the problem) so I need to be able to change the data after building \$\endgroup\$
    – NDraskovic
    Commented Apr 27, 2012 at 10:29
  • \$\begingroup\$ Are you aware of relative paths? I think what you are describing is that you are using absolute file system path to access a file located in your application's folder when you could just access it for example ".\myfile" or just "myfile". Besides that, yes, .net has OpenFileDialog class and you could use it to allow user select a file... \$\endgroup\$
    – Toni
    Commented Apr 27, 2012 at 10:57

1 Answer 1


Just put the Attribute [STAThread] above the Main method in the class Program and add the System.Windows.Forms in the References of the project.

After that you'll be able to instantiate and invoke the dialog boxes.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .