6

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?

EDIT


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?


EDIT 2


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:

enter image description here

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.

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
samy
  • 1,949
  • 6
  • 39
  • 64
  • Could we get a bit more of your code? Like the part you actually do something with that string. Unless this line is exactly where you get an assert? – LightStriker Oct 22 '12 at 20:58
  • 1
    if your link isn't handwritten then I can see atleast 2 flaws. 1. It's called documents and settings and 2. the file name is probably not fourmlinkds. More likely to be fourmlinks. Notice the missing "d" – WozzeC Oct 22 '12 at 21:00
  • @Marc-AndréJutras i added the function that i use to load the file. – samy Oct 22 '12 at 21:01
  • @WozzeC the file name is my mistake, i wrote this question by hand, but i run the same code on all platforms. the other suggestion sound intrstring, how will you change the code? – samy Oct 22 '12 at 21:08
  • 1
    A more up-to-date chart can be found here: [Detect Windows 7 in .net](http://stackoverflow.com/a/2819962/745969) – Tim Oct 22 '12 at 21:42

2 Answers2

3

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();


Running Windows 7 Service Pack 1 32-bit


Thanks
I hope this helps :)

Picrofo Software
  • 5,475
  • 3
  • 23
  • 37
1

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"; 
   } 
}
keyboardP
  • 68,824
  • 13
  • 156
  • 205
  • i did a check 'if (osInfo.Platform == PlatformID.Win32NT)' but i get true on windows 7 too. – samy Oct 22 '12 at 21:26
  • KeyboardP i add'd a check i did, i would love if you will see it and tell me how i can imporve it. – samy Oct 22 '12 at 21:32
  • @samy - You've missed an extra check for XP. I've updated my post but I haven't tested it as I don't have an XP machine to hand. – keyboardP Oct 22 '12 at 21:51
  • this work on windows 7 now but on XP i still get an error, i added the error im reciving (img). – samy Oct 22 '12 at 22:23
  • Sorry, I copy pasted your original code, but you missed out the `Environment.GetFolderPath()` for the XP version. I've updated my post. – keyboardP Oct 22 '12 at 22:46
  • im getting the first error i posted now. any other path ideas for XP? – samy Oct 23 '12 at 18:26
  • its working, i use this for the XP code: System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly(‌​).Location) – samy Oct 23 '12 at 18:42