Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Have you seen the XNA with WinForms examples on the App Hub? Links are in this answer: – George Duckett Apr 27 '12 at 10:15
No I haven't, thanks for the link, I'll take a look when I get the time. – NDraskovic Apr 27 '12 at 10:19
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. – Roy T. Apr 27 '12 at 10:26
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 – NDraskovic Apr 27 '12 at 10:29
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... – Toni Apr 27 '12 at 10:57
up vote 6 down vote accepted

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.