views:

538

answers:

4

I have a little tray application that want's to write to it's own folder under the Program Files directory. I know not an ultimate design and I will fix it, but first I want to understand how this work.

Running this on a 32-bit Vista machine it writes the files to the VirtualStore and it works just like it should.

But when installing this on a Vista 64-bit machine I immediately get hit with a UnauthorizedAccessException for trying to write to the directory within Program Files (and Program Files (x86)).

The VirtualStore redirect does not seem to work on Vista 64-bit. Any ideas?

It's a C# app written in Visual Studio 2008 and I use a FileStream obj to persist the stream to disk.

+1  A: 

Have more info about error ?
Do you use sysinternals tools for monitor execution/access errors ?
Take a look Event viewer for error too.

lsalamon
+1  A: 

I'd just fix it now. You should never have been writing your stuff to that directory anyway, it violates the guidelines. Your product won't work when there's multiple users logged on to the server at the same time, even pre-VISTA.

Vista 64-bit will detect that you're a 32-bit app and will automagically redirect your attempts at modifying "Program Files" to "Program Files x86". In fact, it totally makes you believe you're running on a 32-bit system (see here).

I suspect there's extra protection in this emulation layer from programs trying to change things under Program Files. Or maybe the ACLs are set better under Vista 64-bit (or the emulation layer which is more likely).

Bottom line: don't do what you're doing, Microsoft has been telling us not to do that for a long time now.

paxdiablo
Yes I know and I will rewrite it using AppData or Isolated Storage, but it still bugs the heck out of me that i acts different under 32 and 64 bit. And I don't understand why.
Niklas
See updates for possibilities (or drop Raymond Chen a line over at The Old New Thing, but don't be surprised if he gives you a bit of a taunt about it :-).
paxdiablo
Thanks Pax! The wierd(er) thing is that it still does not work when I compile it for 64-bit and create a 64-bit installer. :(.
Niklas
You may find Vista64 is also protecting Program Files as well as the x86 variant. Can you check the ACLs/permissions for that directory?
paxdiablo
And you may be more admin-y on the 32-bit box.
paxdiablo
A: 

Haven't used any sysinternal tools but it seems pretty clear that it tries to write to the program folder under Program Files.

Here's the Exception:

System.UnauthorizedAccessException was unhandled
  Message="Access to the path 'C:\\Program Files\\PDK Notifieringar\\EventLog.css' is denied."
  Source="mscorlib"
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode)
       at PdkNotifier.WinUI.EmbeddedResources.ResourceCreator.WriteStreamToDisk(Stream streamToPersist, String path)
       at PdkNotifier.WinUI.EmbeddedResources.ResourceCreator.GetEmbeddedResource(String filename)
       at PdkNotifier.WinUI.EmbeddedResources.ResourceCreator.GetEmbeddedResource(String[] filenames)
       at PdkNotifier.WinUI.PdkNotifierApplicationContext.VerifyAndCreateEmeddedResources()
       at PdkNotifier.WinUI.PdkNotifierApplicationContext..ctor()
       at PdkNotifier.WinUI.Program.Main()
  InnerException:
Niklas
A: 

So I actually got this working by compiling all projects to target platform x86. So x64 does not work with VirtualStore on Vista 64 and neither does compiling to "Any CPU". And I had to set it for the entire solution (in the Configuration Manager), just setting it for each individual project didn't work.

Time to rewrite it using AppData folder or IsolatedStorage. Thanks for all the help!

Niklas