Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm creating an app in XNA.

I'm trying to load a custom save file. I named the file with my own custom extension. It's an XML file serialized with the DataContractSerializer.

I've tried several things to load the file. And they all work when I'm running in my development environment, or when the game is running from most folders, but if the game has been placed in the program files folder (or any other protected by Windows 7), the game won't load my custom files, but it will load images and other items that the content importer handles naturally.

Things I have tried:

  • Directly loading the file using the DirectoryInfo.CurrentDirectory() then scanning for files, and loading them.
  • Using the StorageContainer.TitleLocation
  • Creating a custom importer, and loading them into the content pipeline

In all cases, it works fine running in my IDE (C# Express 2008), but when the app is running from Program Files, I get the following error:

Access to the path 'C:\Program Files\MyFolder\MyAppName\Content\CustomFolder\Blah.CustomExtension' is denied.

I am running as administrator (though obviously, I eventually don't want the end user to need this right).

Any suggestions?

share|improve this question
Have you tried running your app as Administrator? –  Arda Xi Aug 25 '10 at 7:32
I assume you're trying this using the standard .Net I/O routines. As an alternate strategy you could try going through the XNA Storage system: msdn.microsoft.com/en-us/library/bb200105.aspx –  Chris Howe Aug 25 '10 at 11:44
@ArdaXi I am running as administrator. (updated question text to reflect this) –  McKay Aug 25 '10 at 14:37
@ChrisHowe I think I tried that, but I'll double check –  McKay Aug 25 '10 at 14:50
@ChrisHowe I tried similar stuff, and I tried looking for specifically that, but the closest I could find was StorageContainer.TitleLocation, which was close, but not file load. Looking through the help file you linked was great, but I couldn't get it to work, because it was XNA 4.0 (still in beta), and using C# Express 2008, is still on XNA 3.1, which doesn't support the awesome TitleContainer. Way good document though. Thanks –  McKay Aug 26 '10 at 4:43

2 Answers 2

up vote 4 down vote accepted

As the commenter said: check to see if your app runs fine when running as administrator, because if it does, that suggests something different.

Otherwise, I think you're running into a straightforward access rights issue. You don't, as a rule, have the right to open any files under "C:\Program Files" with write access. So you don't get to save any files there at all. Your question suggests though that it's not the saving that is causing you problems, but the loading. Still, you need to make absolutely sure that you're not trying to open the files with the wrong access permissions.

You don't say what functions you're actually using to do the loading, we'd need to see that code to confirm. But as an acid test, you should be able to call:

File file = File.Open(path, FileMode.Open, FileAccess.Read);

Which should work, even for files under Program Files.

NB: If you're just using the 'plain' versions of the functions, i.e. omitting the FileAccess.Read parameter, then the system will default to FileAccess.ReadWrite. You will not be allowed to open files using FileAccess.ReadWrite, if they live in the Program Files hierarchy. Even if you never write anything to those files, the open call will fail unless you ask for the right sort of access.

share|improve this answer
I think that I'm only requesting read permission, but I'll double check that. –  McKay Aug 25 '10 at 14:50
I was doing a FileMode.Open, which (IMO) should only request read, but unless you specify, it requests write too by default. –  McKay Aug 26 '10 at 4:47
Well, FileMode is unrelated to what you intend to do with the file, it's just about whether or not the file gets created if it's not there, and whether or not you want to append. It makes no assumptions about whether or not you're opening for reading or writing: it allows both. But yeah, just one of those things that you get bitten by once, and remember for the future. –  MrCranky Aug 26 '10 at 9:04
Yup, it's funny that it's never been a problem before. –  McKay Aug 27 '10 at 16:06

Unless you run the game as Administrator (which you don't want, if only because some players simply won't be able to) you don't permission to write in the Program Files directory.

You should write save games, downloaded maps and other such files somewhere in the directories belonging to the user (Documents, Application Data or something like that).

share|improve this answer
I am not trying to write to the Program Files directory. User save data is done using the StorageDevice classes in XNA. –  McKay Aug 25 '10 at 15:04

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.