My application stores log files in a location which, depending on admin settings, can get redirected to a folder in the VirtualStore. They sometimes end up in, for example:
The log file is in:
C:\Users\-my username-\AppData\Local\VirtualStore\Program Files (x86)\ *my-application* \logs
C# thinks it is here:
C:\Program Files (x86)\ my-application \logs
This is only a problem in one part of the code - a button which tries to open the log file in notepad. It runs Process.Start( path-where-application-thinks-log-files-are );
If I test this using File.Exists( path-where-application-thinks-log-files-are ); I get true - because c# knows to look in the VirtualStore location. But when I try to launch the file, it fails.
So my question is, is there a way to convert a path into the correct location, from the point of view of the Process.Start() command?
The answer to your question is that you cannot.
File and Registry virtualization is a temporary compatibility hack, present in the current version of Windows so that buggy applications will temporarily continue to work. Microsoft provides no capability to cope with redirected files. Applications that do it are in a buggy state and need to be fixed.
From the Developing for Windows blog:
The ideal way to solve your problem is to disable File and Registry Virtualization of your application. That way your application will no longer be able to save files to sensitive locations - and will get an
Access denied
error.You do this by adding an entry to your application's assembly manifest, telling Windows that your application is properly written:
AssemblyManifest.xml
:This way any attempts to write a log file to the
%ProgramFiles%
subtree will correctly fail.Correctly written Windows applications do not store data in
Program Files
. From Technical requirements for the Windows 7 Client Software Logo Program, page 8-9:In your case, log files should either be stored:
LocalAppData
folder (typically resolves toC:\Users\Sugrue\AppData\Local
)CommonAppData
folder (typically resolves toC:\ProgramData
)The choice is yours. Presumably you want a single log file which multiple users can add to. In which case you want the Common AppData folder. You can retrieve this path using
SHGetFolderPath
with theCSIDL_COMMON_APPDATA
, or the newerSHGetKnownFolderPath
:Users are able to write to this folder, because rights to create files and folders are there are granted to Users by default:
In Summary
You can't.
But at the same time: you shouldn't.
Consider what happens if there is no redirection happening. What happens when you run as a standard user on Windows XP?