Thursday, 23 September 2010

Vista & Win 7 - revisited

I've already posted about changes needed for post XP operating systems (ie. Vista & Windows 7). There was, however, an elephant in the room - namely UAC (User Access Control) introduced with Vista.
Now UAC has a lot of aspects, but from a programmer's point of view a major one is needing administrator privileges to write to (make changes to) the Program Files folder. Now, a programmer has a nice warm feeling when all the files that their program needs are in one folder (Program Files > Wessex Pictures in my case). It means they are easy to keep track of and in Windows XP there was no problem setting up your program this way (though whether it was the recommended way is another matter).
Most programs, and the Wessex Pricing Programs are no exception, need to record settings a user makes to configure the program for their use. As far as Wessex Premier & Professional are concerned this includes the labels/values file and the database file itself. Both of these files get written to (ie. changed) a large number of times while the program is in use.
So, these files needed to be set up in another location. The "My Documents" folder is really just too easy to change or "fiddle" with. Microsoft have provided a number of folders that could be used and the one I selected was the Common Application Data folder.
Theoretically, all I had to do was change the sub-routine that returned the path to the Wessex Pictures Program Files folder to point to the Common Application Data folder. Too easy you're thinking - and of course it was. The problem is that in the CommonAppData folder there is a Wessex Pictures folder and within that a WPP4 folder and within that a folder for each "build" of the program, with the code that returns the path to the CommonAppData returning the path to that "build" folder. So what's the problem? Well, if the user installs an updated version of the program it will use a new "build" folder, but the database & user files will be in the previous folder. What is needed is to modify the code to return the folder up from the one returned by the straightforward code (ie. site the files outside the build folders, but inside the WPP4 folder. (see screenshot)


In the above screenshot you can see there are 3 build folders, with the data files outside of them, but within the WPP4 folder.

That crucial line of code is -
Dim dirPathParent As DirectoryInfo = Directory.GetParent(Application.CommonAppDataPath)

It's the "Directory.GetParent" bit that does the business. As with all simple things it took a bit of working out, I couldn't find that anyone else had posted about the problem. What started as some quite elaborate code which searched the folders for the specific files, compared their dates and then moved the latest ones to the new folder, turned into just one extra line.

All that was needed now was some tricky "If" statements checking if the data files were in the old location but not the new location, Then copy the files over. This ensured that users updating the program weren't involved in complex file copying in places they didn't know existed on their computer.

Does it work? It was with some trepidation I installed the update at work (after all it's one thing working in the programming environment and quite another using and letting others use the program for real). I double-clicked the icon on the desktop - the program started and I quickly checked through the database & values - it was all there, pheeew!
This will be the version (4.1.0) that will, finally, be on the Wessex Pictures website for download.

No comments:

Post a Comment