6

我的应用程序附带的文本文件有一个奇怪的问题。

该文件包含一堆站点,当程序启动时,它将站点加载到一个数组中。

在 Windows 7 上,当我启动应用程序时,我没有收到任何错误。然而,在 XP 上我得到c:\Document and setting\I\Application Data\fourmlinks.txt file not found. 了奇怪的部分是我用它制作了一个包含内容的文本文件,并将它放在应用程序文件夹中。

这就是我在代码中调用的方式:

string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\fourmlinks.txt";

我的问题是我无法创建新文件,因为它包含应用程序需要和正在使用的基本数据。

首次启动后,用户可以根据需要编辑文件。

我不确定为什么会发生这种情况,但这只发生在 Windows XP 上。

我怎么解决这个问题?

编辑


keyboardP 建议检查正在运行的窗口,然后通过它更改路径。所以我想出了这个代码:

 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";

问题是即使在 Windows 7 上我也得到了真实,当我需要得到错误时。有没有办法确保我在 XP 或 Windows 7 上以不同的方式运行?


编辑 2


使用操作系统的检查,我现在可以确定我是 Windows 7 或 Windows XP。因此,代码在 Windows 7 上再次运行 find 但在 Windows XP 上我收到不同的错误消息:

在此处输入图像描述

我真的不知道我在程序中添加的路径如何成为错误所说的我正在请求的路径。

4

2 回答 2

3

要检测用户正在运行的当前操作系统,您可以使用System.OperatingSystem由映射到以下 Windows 版本的三个组件组成的操作系统:

+-----------------------------------------------------------------------------------------------------------------------------------------+
|           |   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    |
+-----------------------------------------------------------------------------------------------------------------------------------------+

知道MajorandMinor版本就足够了,因为PlatFormIDis Win32WindowsorWin32NT

下面的例子展示了如何使用OperatingSystem来检测用户当前的操作系统。

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;
}

使用上面发布的示例,例如,Windows XP Service Pack 3如果用户正在运行 Windows XP Service Pack 3,如果您调用getOSInfo();


运行 Windows 7 Service Pack 1 32 位


谢谢
我希望这会有所帮助:)

于 2012-10-22T21:48:12.120 回答
1

在 XP 上,尝试使用Environment.SpecialFolder.LocalApplicationData.

从评论编辑

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"; 
   } 
}
于 2012-10-22T21:03:23.060 回答