c# - Environment.GetFolderPath(...CommonApplicationData) 在 Vista 上仍然返回 "C:\Documents and Settings\"

标签 c# .net

据我了解,您应该使用 Environment.GetFolderPath 方法,这样您就有了独立于操作系统的代码...

Windows XP 使用 C:\Documents and Settings\。 Windows Vista 使用 C:\ProgramDataC:\Users

我在 Windows Vista 计算机上使用下面的代码,它返回一个 C:\Documents and Settings\ 目录而不是像它应该的那样 C:\ProgramData ...有什么想法吗?

    string commonAppData = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
    try
    {
        File.CreateText(
            Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) +
            "\\mycompany\\uid");
        log.Debug("Created file successfully");
    }
    catch (Exception ex)
    {
        log.Error("Unable to create the uid file: ", ex);
    }

最佳答案

我的安装程序复制了一个在 XP 计算机上生成的 log.txt 文件。我正在查看该日志文件,认为它是在 Vista 上生成的。一旦我将 log4net 配置修复为“Vista 兼容”。 Environment.GetFolderPath 返回预期结果。因此,我要关闭这篇文章。

以下 SpecialFolder 路径引用可能有用:

Windows Server 2003 上的输出:

SpecialFolder.ApplicationData: C:\Documents and Settings\blake\Application Data
SpecialFolder.CommonApplicationData: C:\Documents and Settings\All Users\Application Data
SpecialFolder.ProgramFiles: C:\Program Files
SpecialFolder.CommonProgramFiles: C:\Program Files\Common Files
SpecialFolder.DesktopDirectory: C:\Documents and Settings\blake\Desktop
SpecialFolder.LocalApplicationData: C:\Documents and Settings\blake\Local Settings\Application Data
SpecialFolder.MyDocuments: C:\Documents and Settings\blake\My Documents
SpecialFolder.System: C:\WINDOWS\system32`

Vista 上的输出:

SpecialFolder.ApplicationData: C:\Users\blake\AppData\Roaming
SpecialFolder.CommonApplicationData: C:\ProgramData
SpecialFolder.ProgramFiles: C:\Program Files
SpecialFolder.CommonProgramFiles: C:\Program Files\Common Files
SpecialFolder.DesktopDirectory: C:\Users\blake\Desktop
SpecialFolder.LocalApplicationData: C:\Users\blake\AppData\Local
SpecialFolder.MyDocuments: C:\Users\blake\Documents
SpecialFolder.System: C:\Windows\system32

关于c# - Environment.GetFolderPath(...CommonApplicationData) 在 Vista 上仍然返回 "C:\Documents and Settings\",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/895723/

相关文章:

C# Regex 匹配不包含特定字符串的字符串?

.net - CopyPixelOperation.SourceInvert 不起作用

c# - 使用 HTML Agility Pack 获取内部文本

.net - 使用 Entity Framework 更新主键值

c# - 不让我使用秒表类。在 Visual Studio

c# - 加载 Ninject 模块后设置所有绑定(bind)的范围

c# - 使用 CookieAuthenticationProvider 调用 AuthenticationManager.SignIn() 后 ClaimsIdentity 信息存储在哪里

c# - 为什么我可以在 c#7.3 中将 ref 结构声明为类的成员?

C# - 如何使 AutoMapper 不区分大小写?

c# - 在不同的程序集中运行测试