gpt4 book ai didi

c# - 在 xp 上找不到文件

转载 作者:太空狗 更新时间:2023-10-29 21:45:49 26 4
gpt4 key购买 nike

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

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

在 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 上我也得到 true 的问题,当我需要得到 false 时。有没有办法确保我以不同的方式在 XP 或 Windows 7 上运行?


编辑 2


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

enter image description here

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

最佳答案

要检测用户当前运行的操作系统,您可以使用 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 |
+-----------------------------------------------------------------------------------------------------------------------------------------+

知道 MajorMinor 版本就足够了,因为 PlatFormIDWin32WindowsWin32NT

以下示例显示了如何使用 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;
}

使用上面发布的示例,如果您调用 getOSInfo();


Running Windows 7 Service Pack 1 32-bit


谢谢
希望对您有所帮助 :)

关于c# - 在 xp 上找不到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13019715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com