6
\$\begingroup\$

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?

\$\endgroup\$
6
  • 1
    \$\begingroup\$ Have you tried running your app as Administrator? \$\endgroup\$
    – Arda Xi
    Commented Aug 25, 2010 at 7:32
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – Chris Howe
    Commented Aug 25, 2010 at 11:44
  • \$\begingroup\$ @ArdaXi I am running as administrator. (updated question text to reflect this) \$\endgroup\$
    – McKay
    Commented Aug 25, 2010 at 14:37
  • \$\begingroup\$ @ChrisHowe I think I tried that, but I'll double check \$\endgroup\$
    – McKay
    Commented Aug 25, 2010 at 14:50
  • \$\begingroup\$ @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 \$\endgroup\$
    – McKay
    Commented Aug 26, 2010 at 4:43

2 Answers 2

5
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ I think that I'm only requesting read permission, but I'll double check that. \$\endgroup\$
    – McKay
    Commented Aug 25, 2010 at 14:50
  • \$\begingroup\$ I was doing a FileMode.Open, which (IMO) should only request read, but unless you specify, it requests write too by default. \$\endgroup\$
    – McKay
    Commented Aug 26, 2010 at 4:47
  • \$\begingroup\$ 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. \$\endgroup\$
    – MrCranky
    Commented Aug 26, 2010 at 9:04
  • \$\begingroup\$ Yup, it's funny that it's never been a problem before. \$\endgroup\$
    – McKay
    Commented Aug 27, 2010 at 16:06
1
\$\begingroup\$

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).

\$\endgroup\$
1
  • \$\begingroup\$ I am not trying to write to the Program Files directory. User save data is done using the StorageDevice classes in XNA. \$\endgroup\$
    – McKay
    Commented Aug 25, 2010 at 15:04

You must log in to answer this question.

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