I have a weird problem with a text file that I ship with my app.
The file holds a bunch of sites, when the program starts it loads the sites into an array.
On windows 7, when I start the app I don't get any errors. However, on XP I get c:\Document and setting\I\Application Data\fourmlinks.txt file not found.
The weird part is that I made a text file with content with it, and I put it inside the application folder.
This is how I call out in my code:
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\fourmlinks.txt";
My problem is that I can't create a new file because it is holding basic data that the app needs and is using.
After the first launch the user can edit the file as much he wants.
I'm not sure why this is happening, but this is only happening on Windows XP.
How can I solve this problem?
keyboardP suggest to check on what windows im runing and then change the path by it. so i came up with this code:
System.OperatingSystem osInfo = System.Environment.OSVersion;
if (osInfo.Platform == PlatformID.Win32NT)
path = Environment.SpecialFolder.LocalApplicationData + "\\fourmlinks.txt";
else
path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\fourmlinks.txt";
the problem that even on windows 7 i get true, when i need to get false. is there a way to make sure i run on XP or Windows 7 i diffrent way?
using the check of the Operating System, I can now be sure I'm Windows 7 or Windows XP. So, the code run find again on Windows 7 but on Windows XP I get a different error message:
I really have no idea how the path that I add in my program becomes the path that the error is saying I'm requesting.
To detect the current Operating System the user is running, you may use System.OperatingSystem
which consists of THREE components which map to the following Windows versions:
+-----------------------------------------------------------------------------------------------------------------------------------------+
| | Windows | Windows | Windows |Windows NT| Windows | Windows | Windows | Windows | Windows | Windows | Windows |
| | 95 | 98 | Me | 4.0 | 2000 | XP | 2003 | Vista | 2008 | 7 | 2008 R2 |
+-----------------------------------------------------------------------------------------------------------------------------------------+
|PlatformID | Win32Windows | Win32Windows | Win32Windows | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT |
+-----------------------------------------------------------------------------------------------------------------------------------------+
|Major | | | | | | | | | | | |
| version | 4 | 4 | 4 | 4 | 5 | 5 | 5 | 6 | 6 | 6 | 6 |
+-----------------------------------------------------------------------------------------------------------------------------------------+
|Minor | | | | | | | | | | | |
| version | 0 | 10 | 90 | 0 | 0 | 1 | 2 | 0 | 0 | 1 | 1 |
+-----------------------------------------------------------------------------------------------------------------------------------------+
It's enough to know the Major
and Minor
versions since the PlatFormID
is Win32Windows
or Win32NT
The following example shows how to use OperatingSystem
to detect the user's current operating system.
int getOSArchitecture()
{
//Only required if you would like to show the user's processor architecture (32-bit / 64-bit)
string pa = Environment.GetEnvironmentVariable("PROCESSOR_ARCHITECTURE");
return ((String.IsNullOrEmpty(pa) || String.Compare(pa, 0, "x86", 0, 3, true) == 0) ? 32 : 64);
}
string getOSInfo()
{
//Get Operating system information.
OperatingSystem os = Environment.OSVersion;
//Get version information about the os.
Version vs = os.Version;
//Variable to hold our return value
string operatingSystem = "";
if (os.Platform == PlatformID.Win32Windows)
{
//This is a pre-NT version of Windows
switch (vs.Minor)
{
case 0:
operatingSystem = "95";
break;
case 10:
if (vs.Revision.ToString() == "2222A")
operatingSystem = "98SE";
else
operatingSystem = "98";
break;
case 90:
operatingSystem = "Me";
break;
default:
break;
}
}
else if (os.Platform == PlatformID.Win32NT)
{
switch (vs.Major)
{
case 3:
operatingSystem = "NT 3.51";
break;
case 4:
operatingSystem = "NT 4.0";
break;
case 5:
if (vs.Minor == 0)
operatingSystem = "2000";
else
operatingSystem = "XP";
break;
case 6:
if (vs.Minor == 0)
operatingSystem = "Vista";
else
operatingSystem = "7";
break;
default:
break;
}
}
//Make sure we actually got something in our OS check
//We don't want to just return " Service Pack 2" or " 32-bit"
//That information is useless without the OS version.
if (operatingSystem != "")
{
//Got something. Let's prepend "Windows" and get more info.
operatingSystem = "Windows " + operatingSystem;
//See if there's a service pack installed.
if (os.ServicePack != "")
{
//Append it to the OS name. i.e. "Windows XP Service Pack 3"
operatingSystem += " " + os.ServicePack;
}
//Append the OS architecture. i.e. "Windows XP Service Pack 3 32-bit"
operatingSystem += " " + getOSArchitecture().ToString() + "-bit"; //Remove this if you do not want to show the processor architecture
}
//Return the information we've gathered.
return operatingSystem;
}
Using the example posted above, you'll get for example, Windows XP Service Pack 3
if the user is running Windows XP Service Pack 3 if you call getOSInfo();
Thanks
I hope this helps :)
On XP, try using Environment.SpecialFolder.LocalApplicationData
.
Edit from comments
path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\fourmlinks.txt";
System.OperatingSystem osInfo = System.Environment.OSVersion;
if (osInfo.Platform == PlatformID.Win32NT)
{
if(osInfo.Version.Major == 5 && osInfo.Version.Minor != 0)
{
//running XP
path = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\fourmlinks.txt";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With