我的应用附带的文本文件存在一个奇怪的问题。
该文件包含一堆站点,当程序启动时,它将站点加载到一个数组中。
在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上,我收到一条不同的错误消息:
我真的不知道我在程序中添加的路径如何成为错误说我正在请求的路径。
答案 0 :(得分: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 |
+-----------------------------------------------------------------------------------------------------------------------------------------+
由于Major
为Minor
或PlatFormID
Win32Windows
和Win32NT
版本就足够了
以下示例说明如何使用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();
由于 我希望这有助于:)
答案 1 :(得分: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";
}
}