Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have some code that is reading a config file, but when I open the file in TextPad, I see different values than my application does. I checked it with Notepad. Notepad agrees with my application, TextPad shows something else.

This is on Vista x64 Business.

Any idea what could be causing this? I've looked in the Context Menu->Properties->Previous Versions details, but it says "There are no previous versions available".

I've been asked for an example, here's the steps to replicate (I can't make it happen reliably):

  1. Installed .NET app in Program files.
  2. That app reads the config file, but is falling over.
  3. I manually edit that config file in Textpad.
  4. The change doesn't take effect.
  5. I open the config file in Notepad, and see something different.
  6. Try making the change in Notepad and saving, and get this error message:

Notepad

Cannot create the C:\Program Files (x86)\Daniel Schaffer\WorkingOn for FogBugz\FogBugz > WorkingOn.exe.config file.

Make sure that the path and file name are correct.

I can't put the content here as it's XML and is being encoded by Superuser.com, but here's a link to a screencast: http://screencast.com/t/zhERl7mocp4.

share|improve this question
    
Can you post an example. Without more details it's going to be hard to diagnose the problem –  ChrisF Sep 25 '09 at 11:21
    
Thanks for looking - I've updated the question. –  Neil Barnwell Sep 25 '09 at 11:31
    
Also asked on Stackoverflow - stackoverflow.com/questions/1476870/… –  ChrisF Sep 25 '09 at 12:14
    
If you mark the XML as code it should get posted as is. –  ChrisF Sep 25 '09 at 12:15

2 Answers 2

up vote 3 down vote accepted

You're probably running into a part of Vista's UAC features called Virtual Store. Here's a snippet from the article:

When an application writes to a system location only writeable by administrators, Windows then writes all subsequent file operations to a user-specific path under the Virtual Store directory, which is located at %LOCALAPPDATA%\VirtualStore. Later, when the application reads back this file, the computer will provide the one in the Virtual Store. Because the Windows security infrastructure processes the virtualization without the application’s assistance, the application believes it was able to successfully read and write directly to Program Files. The transparency of file virtualization enables applications to perceive that they are writing and reading from the protected resource, when in fact they are accessing the virtualized version.

Your application is running in a non-elevated (non-admin) context. So when it tries to open the file in the Program Files folder, that request is getting redirected to your user accounts Virtual Store location transparently. Your copy of TextPad is probably running in an elevated context and so it is seeing the file that lives in the real Program Files folder.

If this is your code that you can change the functionality of and you want users to be able to use it without running as admin, you should change the location of where you store your config files to somewhere in the user's ApplicationData folder where it belongs. Writing settings to a config file stored in the application's installation folder is highly discouraged these days. If you really have settings that should be applying to all users on the machine, you should write them to the All Users Application Data folder instead. And even then, you'll still have to run the app in an Elevated context by default unless you explicitly grant the Users group write access to your folder.

Just keep this mantra going in your head as you write software for Vista and beyond. "Users can only write to their profile...Users can only write to their profile"

share|improve this answer

The reason why Notepad can't create the file is that another process is locking it. Since the content of the file changes, my guess would be that the app is writing the file while you look at it.

share|improve this answer
    
But how does that explain that textpad opening the file and notepad opening the same file are getting different text? –  Neil Barnwell Sep 25 '09 at 12:36
1  
Textpad reads the file, app changes the file, Notepad reads the new file –  Aaron Digulla Sep 25 '09 at 12:56
    
If your app isn't running, then this behavior is extremely odd. Install "ProcessExplorer" and use "Find handle" with the filename when Notepad refuses to save the file to find the process which is locking it. –  Aaron Digulla Sep 25 '09 at 13:28

Your Answer

 
discard

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.