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?
Asked
Active
Viewed 2,457 times
2
-
2Have you seen the XNA with WinForms examples on the App Hub? Links are in this answer: http://stackoverflow.com/a/6558501/593627 – 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
-
Of course, this is a problem if you intend to deploy to xbox which does not support system.windows.forms namespace – Toni Apr 27 '12 at 11:09
-
This is not a problem, since this application is intended for educational purposes and will be developed only for PC-s (with windows OS). I've tried with relative paths, but then the modifications to the file do not manifest themselves (currently the file is located on my desktop, and the application works perfectly) – NDraskovic Apr 27 '12 at 11:22
-
If relative paths didn't work you should reexamine them because they should work, also think again if it's a good idea to have your config file on the desktop and not in a subfolder of your program. Yet another solution would be to add an app.config file to your project and load data from it using ConfigurationManager.AppSettings (http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.appsettings.aspx) – Roy T. Apr 27 '12 at 12:34
-
I don't think you understand me - this file I use only provides informations on where and which objects will be drawn to the screen (that is one of the basic requirements for this application). Think of it as a very simplified level designer. I need to enable the user to write his design/configuration (call it what you want) in a naotepad or similar text editor and then pass the path of that file to my application so it can be drawn in a way he wanted – NDraskovic Apr 27 '12 at 13:32
1 Answers
6
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.

Emir Lima
- 457
- 2
- 8