I am working on a program that is designed to operate in conjunction with an old C++ application. A game called Age of Wonders. When running under Windows Vista or Windows 7 (with the UAC enabled) the game save files are written to the virtualisation path instead of the real path.
So for example;
Original: C:\Program Files (x86)\Age of Wonders\Save
Virtualized: C:\Users\UserName\AppData\Local\VirtualStore\Program Files (x86)\Age of Wonders\Save
In my .Net application I download files from an email server and place them in the Save folder, if I try to write to the original path I receive an Unauthorised Access Exception when the UAC is enabled. Windows doesn’t automatically convert it to the Virtualised path for me. I’ve been working around this by getting my users to run the application as administrator. However I would like to make a more elegant solution.
I could write some code to handle the exception and write to the virtualised path in my code, but I think a better solution would be to somehow switch my program into a mode so that this is done by Windows itself and not in my code. This I feel would be a better long term solution for future versions of Windows.
I have spent time searching on the internet and I have found other people who are talking about this, but no one provides any help which is actually usable. Here are the links I have looked at;
Should I use a VirtualStore solution on Vista?
Create Process with FS Virtualization Enabled
I need to solution to not involve the user having to modify their system settings or create any accounts to run the process under etc.
So, below I have the code for a simple Windows Forms app. It has one button and one check box, the check box is intended to switch the virtualisation mode and the button writes a small text file to the a Program Files folder. I want to be able to use this to test the virtualisation behaviour. So I would expect the File.txt to be written to the virtualised path when the check box is checked etc.
If anyone can help me fill in the blank function I would be extremely grateful. Thanks in advance.
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string testText = "Dave was here";
File.WriteAllText("C:\\Program Files\\DaveTest\\File.txt", testText);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
SetVirtualization(checkBox1.Checked);
}
private void SetVirtualization(bool enabled)
{
//What code do I need here, please provide examples than can be used
}
}