C# get location of knownfolder by ID
Asked Answered
M

2

13

I want to get the location of a directory/folder by its ID.
For example, the Downloads folder has the ID knownfolder:{374DE290-123F-4565-9164-39C4925E467B}, when I enter it into the address bar of windows explorer, it redirects my to the downloads folder.

enter image description here

There is a list of these IDs and the corresponding folders here, so I could hardcode the IDs and look them up like this, but I wouldnt want to do this unless its the only way.

Is there another way to properly get what I want?

Metallography answered 16/9, 2016 at 13:11 Comment(0)
A
16

Stolen from here. Looking at this further, the only way to do it is using a WinAPI/PInvoke

public static class KnownFolderFinder
{
    private static readonly Guid CommonDocumentsGuid = new Guid("ED4824AF-DCE4-45A8-81E2-FC7965083634");

    [Flags]
    public enum KnownFolderFlag : uint
    {
        None = 0x0,
        CREATE = 0x8000,
        DONT_VERFIY = 0x4000,
        DONT_UNEXPAND= 0x2000,
        NO_ALIAS = 0x1000,
        INIT = 0x800,
        DEFAULT_PATH = 0x400,
        NOT_PARENT_RELATIVE = 0x200,
        SIMPLE_IDLIST = 0x100,
        ALIAS_ONLY = 0x80000000
    }

    [DllImport("shell32.dll")]
    static extern int SHGetKnownFolderPath([MarshalAs(UnmanagedType.LPStruct)] Guid rfid, uint dwFlags, IntPtr hToken, out IntPtr pszPath);

    public static string GetFolderFromKnownFolderGUID(Guid guid)
    {
        return pinvokePath(guid, KnownFolderFlag.DEFAULT_PATH);
    }

    public static void EnumerateKnownFolders()
    {
        KnownFolderFlag[] flags = new KnownFolderFlag[] {
            KnownFolderFlag.None,
            KnownFolderFlag.ALIAS_ONLY | KnownFolderFlag.DONT_VERFIY,
            KnownFolderFlag.DEFAULT_PATH | KnownFolderFlag.NOT_PARENT_RELATIVE,
        };


        foreach (var flag in flags)
        {
            Console.WriteLine(string.Format("{0}; P/Invoke==>{1}", flag, pinvokePath(CommonDocumentsGuid, flag)));
        }
        Console.ReadLine();
    }

    private static string pinvokePath(Guid guid, KnownFolderFlag flags)
    {
        IntPtr pPath;
        SHGetKnownFolderPath(guid, (uint)flags, IntPtr.Zero, out pPath); // public documents

        string path = System.Runtime.InteropServices.Marshal.PtrToStringUni(pPath);
        System.Runtime.InteropServices.Marshal.FreeCoTaskMem(pPath);
        return path;
    }
}

And you could then call something like this:

var folder = KnownFolderFinder.GetFolderFromKnownFolderGUID(new Guid("374DE290-123F-4565-9164-39C4925E467B");
Agate answered 16/9, 2016 at 13:28 Comment(0)
P
-4

I think you're looking for Environment.SpecialFolder (System Namespace):

https://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.110).aspx

 // Sample for the Environment.GetFolderPath method
using System;

class Sample 
{
    public static void Main() 
{
Console.WriteLine();
Console.WriteLine("GetFolderPath: {0}", 
             Environment.GetFolderPath(Environment.SpecialFolder.System));
}
  }
/*
This example produces the following results:

GetFolderPath: C:\WINNT\System32
*/
Petrography answered 16/9, 2016 at 13:17 Comment(8)
"I want to get the location of a directory/folder by its ID." Yours doesn't take the known folder ID...Ermentrude
"but I wouldnt want to do this unless its the only way. Is there another way to properly get what I want?"Petrography
"so I could hardcode the IDs and look them up like this, but I wouldnt want to do this unless its the only way." <--- the bolded is what he wouldn't want to do. The title of the question is "get location of knownfolder by ID". It's pretty unambiguous.Ermentrude
Yeah, and my answer provides a reasonable alternative. I suppose we're reading it differently - it's up to OP to clarify what intent of post was.Petrography
The OP does not need to clarify. It's perfectly clear: he has a set of folder IDs and wants to translate them into a folder. He's not starting with any other information.Ermentrude
This is a perfectly viable answer, given that the question clearly says "so I could hardcode the IDs and look them up like this, but I wouldnt want to do this unless its the only way.". Without hardcoding them (which he clearly doesn't want to do), how else can he do it? Hardcoding them is not the only way, there's a special folder enumeration for that. OP also didn't specify which, if any, folders outside of Downloads he wants to use which IIRC is in the SpecialFolders enum.Coot
@Coot -- In order to use the enumeration, you have to know what folders you have at design time. You guys are both missing the point: given a folder ID values which is dynamically provided at run time, how do you translate it into a folder? Having folder enums doesn't help you.Ermentrude
I don't see how this helps the OP that basically wants a function like getFolderPathFromGUID(string guid). Also do note this is discussed hereFriesian

© 2022 - 2024 — McMap. All rights reserved.