In the second edition of PED we introduce a new area; Creating Cross Version Applications. That is, to develop solutions that must run with all Excel versions and Windows versions using a single code base. I will not discuss how to code against different Excel versions. Instead I set focus on running .NET solutions on different Windows versions, i.e. Windows 2000/Windows XP and Windows Vista/Windows 7. It should be noted that I have previously blogged how to check which Excel version that is installed.
With Windows 2000 and Windows XP we know that the user accounts are available in paths like:
C:\Documents and Settings\Dennis
In Windows Vista and Windows 7 the user accounts are located in paths like:
Given this information we can rather easily code for different Windows versions in VB.NET. First we need to find out which Windows version that the targeting computers use. As the code shows below the namespace My.Computer once again comes to our rescue.
In the next step we retrieve the current user’s identity which is carried out by the SystemInformation namespace. Finally we put it together with the help from the StringBuilder namespace.
When we run the code on a Windows Vista configuration we get the following information:
When the User Access Control (UAC) is turned on (Windows Vista/Windows 7) we cannot access the c:\ root unless we run as administrator. But no matter the UAC status we can always access the current user’s folder system. In that way we can write and read files to the user’s folders in addition to the above.