Search code examples
c#.netspecial-folders

Environment.GetFolderPath(...CommonApplicationData) is still returning "C:\Documents and Settings\" on Vista


As I understand it, you're supposed to use the Environment.GetFolderPath method so you have OS independent code...

Windows XP uses C:\Documents and Settings\. Windows Vista uses C:\ProgramData and C:\Users.

I'm using the code below on a Windows Vista computer and it's returning a C:\Documents and Settings\ directory instead of C:\ProgramData like it should... Any ideas?

    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);
    }

Solution

  • My installer copied a log.txt file which had been generated on an XP computer. I was looking at that log file thinking it was generated on Vista. Once I fixed my log4net configuration to be "Vista Compatible". Environment.GetFolderPath was returning the expected results. Therefore, I'm closing this post.

    The following SpecialFolder path reference might be useful:

    Output On 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`
    

    Output on 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