C# 在xp上找不到文件

C# 在xp上找不到文件,c#,winforms,file,text,text-files,C#,Winforms,File,Text,Text Files,我的应用程序附带的文本文件有一个奇怪的问题 该文件包含一组站点,当程序启动时,它会将这些站点加载到一个数组中 在Windows7上,当我启动应用程序时,我没有收到任何错误。但是,在XP上,我找不到c:\Document and setting\I\Application Data\fourmlinks.txt文件。 奇怪的是,我制作了一个包含内容的文本文件,并将其放在应用程序文件夹中 这是我在代码中调用的方式: string path = Environment.GetFolderPath(En

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

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

在Windows7上,当我启动应用程序时,我没有收到任何错误。但是,在XP上,我找不到
c:\Document and setting\I\Application Data\fourmlinks.txt文件。
奇怪的是,我制作了一个包含内容的文本文件,并将其放在应用程序文件夹中

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

string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\fourmlinks.txt";
我的问题是,我无法创建新文件,因为它包含应用程序需要和正在使用的基本数据

在第一次启动后,用户可以根据需要编辑文件

我不知道为什么会发生这种情况,但这只发生在Windows XP上

我怎样才能解决这个问题

编辑
键盘p建议检查正在运行的windows,然后通过它更改路径。 所以我想出了这个代码:

 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";
问题是,即使在Windows7上,当我需要设置为false时,我也会设置为true。 有没有办法确保我以不同的方式在XP或Windows7i上运行


编辑2
通过检查操作系统,我现在可以确定我是Windows 7或Windows XP。 因此,在Windows 7上再次运行“查找”代码,但在Windows XP上,我收到一条不同的错误消息:


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

在XP上,尝试使用
环境.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"; 
   } 
}

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

+-----------------------------------------------------------------------------------------------------------------------------------------+
||窗|窗|窗|窗|窗|窗|窗|窗|窗|窗|
|| 95 | 98 | Me | 4.0 | 2000 | XP | 2003 | Vista | 2008 | 7 | 2008 R2|
+-----------------------------------------------------------------------------------------------------------------------------------------+
|平台化| Win32Windows | Win32Windows | Win32Windows | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT | Win32NT|
+-----------------------------------------------------------------------------------------------------------------------------------------+
|少校|
|版本| 4 | 4 | 4 | 5 | 5 | 6 | 6 | 6|
+-----------------------------------------------------------------------------------------------------------------------------------------+
|小调|
|版本| 0 | 10 | 90 | 0 | 0 | 1 | 2 | 0 | 0 | 1 | 1|
+-----------------------------------------------------------------------------------------------------------------------------------------+
由于
PlatFormID
Win32Windows
Win32NT

以下示例显示如何使用
操作系统
检测用户的当前操作系统

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,如果调用
getOSInfo(),您将得到
Windows XP Service Pack 3



谢谢

我希望这能有所帮助:)

我们能多了解一下您的代码吗?就像你用那根绳子做的一样。除非这一行正好是你得到断言的地方?如果你的链接不是手写的,那么我至少可以看到2个缺陷。1.它被称为文档和设置以及2。文件名可能不是fourmlinkds。更有可能是Formlinks。请注意缺少的“d”@Marc AndréJutras我添加了用于加载文件的函数。@WozzeC文件名是我的错误,我手工编写了这个问题,但我在所有平台上运行相同的代码。另一个建议听起来很有趣,你将如何更改代码?这里可以找到一个更为最新的图表:我做了一个检查“如果(osInfo.PlatformID==PlatformID.Win32NT)”,但我在windows 7上也实现了。键盘P我添加了一个我做过的检查,如果你能看到它并告诉我如何导入它,我会很高兴的。@samy-你错过了一个额外的XP检查。我已经更新了我的帖子,但我还没有测试它,因为我手头没有XP机器。现在在windows 7上进行此操作,但在XP上我仍然收到一个错误,我添加了错误imreceiving(img)。很抱歉,我复制粘贴了您的原始代码,但是您错过了XP版本的
环境。GetFolderPath()
。我已经更新了我的帖子。我想感谢你提供的信息,我以后会使用它:)@samy一点问题都没有,我很高兴能帮上忙:)