sam*_*amy 6 c# text file text-files winforms
我的应用附带的文本文件有一个奇怪的问题.
该文件包含一堆站点,当程序启动时,它将站点加载到一个数组中.
在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上以不同的方式运行?
使用操作系统的检查,我现在可以确定我是Windows 7或Windows XP.因此,代码运行在Windows 7上再次查找,但在Windows XP上,我收到一条不同的错误消息:

我真的不知道我在程序中添加的路径如何成为错误说我要求的路径.
在 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"; 
   } 
}