Search code examples
c#.netwindowsspecial-folderssystem32

Difference between Environment.GetFolderPath(Environment.SpecialFolder.System) and Environment.SystemDirectory for System32 folder


I want to get the path system32 path on 32 bit Windows and SysWOW64 on 64 bit Windows. Both Environment.GetFolderPath(Environment.SpecialFolder.System) and Environment.SystemDirectory work fine. Which one should I prefer and use? I am using .NET 2.0


Solution

  • You could prefer using

    Environment.SystemDirectory
    

    this is because, the .NET framework already knows what folder you need and under the covers makes a direct call to

    Win32Native.GetSystemDirectory
    

    from the underlying kernerl32.dll.. (and then does a quick permission check)

    if you use,

    Environment.GetFolderPath
    

    then you have to pass in an enum, and under the covers the GetFolderPath has to do a quick enum validity check. Once done, it calls the native

    Win32Native.SHGetFolderPath
    

    passing in the integer equivalent of the type of folder needed.

    this calls the underlying method from the native shfolder.dll and this method again would have a switch/case like check based on the folder requested and finally invoke the system directory logic.

    based on the above 2 flows, the

    Environment.SystemDirectory
    

    should be more suited and probably quicker for you. (i haven't really measured)