0

I use SHGetSpecialFolderLocation() to determine the IDCList entry for a special directory:

_ = Win32API.Shell32.SHGetSpecialFolderLocation(hWndWindow, (int)RootDirectory, out IntPtr pidlRoot); // Determine IDCList for the root directory via a special directory listing

I then display it as the root directory in a directory dialog using SHBrowseForFolder():

pidlRet = Win32API.Shell32.SHBrowseForFolder(ref bi); // Show directory dialog

However, when I run this for e.g. CSIDL_MYDOCUMENTS (0x005), the root directory points to an empty directory structure, although there are subdirectories in MyDocuments (see image).

The return value (PIDCList) of the directory dialog whose directory I'm querying with SHGetPathFromIDList():

SHGetPathFromIDList(IDList, sb))

refers to a directory that does not exist:

C:\Users\fred.aurich\Documents

the user document directory is:

C:\Benutzer\fred.aurich\Dokumente

Is that possibly the cause? Or how do I get the subfolders displayed in the directory dialog via CSIDL_MYDOCUMENTS???

Folder Browser and result:

image

Inclusion of the Forms assembly:

image

  • Internet research
  • Display of subfolders
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
perlfred
  • 1
  • 3
  • 1
    From the docs "As of Windows Vista, these values have been replaced by KNOWNFOLDERID values. See that topic for a list of the new constants and their corresponding CSIDL values." So you need to use `SHGetKnownFolderPath`. Also why don't you just use `Environment.GetFolderPath` and `FolderBrowserDialog` which does all this for you? – Charlieface Jul 06 '23 at 10:39
  • @Charlieface Hello Charlieface! **Thanks** for your hints! I can't use the FolderBrowserDialog **anymore** because the **Forms** NameSpace can only be integrated very poorly in a WPF Dot-Net Core project! SHGetKnownFolderPath doesn't give me a (pointer to) a PIDCList object that I can then pass to the BROWSEINFO structure of the pidlRoot property to ultimately pass to the SHBrowseForFolder method. Is there perhaps a conversion from a pointer to a path (*ppszPath) to a PIDCList entry? – perlfred Jul 06 '23 at 13:17
  • I found something now: `ILCreateFromPath(shlobj_core.h)` in conjunction with `Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments)` should implement my requirement. – perlfred Jul 06 '23 at 13:54
  • I'm not really convinced about your insistence "the Forms NameSpace can only be integrated very poorly in a WPF Dot-Net Core project", it's just a matter of adding the right package and reference. But have you tried with the `FOLDERID_Documents` Guid using `SHGetKnownFolderIDList`? Yes you can use `ILCreateFromPath` as well. I hope you are freeing the `PIDCList` using `Marshal.FreeCoTaskMem` – Charlieface Jul 06 '23 at 13:54
  • To quickly explain the problem of integrating Forms-NameSpace: The solution should be: 1. Add the entry: true to the project file. 2. Add the FormsNamespace to the project. to 1. My project file looks like this: WinExe net5.0-windows true true DVD-Management-2.ico End Part 1 – perlfred Jul 06 '23 at 14:19
  • And what happened when you did that, did it not work? Also have you considered upgrading to a supported version of .NET such as .NET 6 – Charlieface Jul 06 '23 at 14:20
  • Part 2: to 2. When adding the assembly reference, there is only the entry: System_Windows_Forms with underscores instead of dots and the assembly refers to a *.tlb instead of *.dll. If you have integrated this into the project, you will get the error message (the next time you open the project): Warning MSB3290 Failed to build wrapper assembly for type library "{215d64d2-031c-33c7-96e3-61794cd1ee61}". The "System_Windows_Forms" type library cannot be imported as a CLR assembly because it was exported from a CLR assembly. And the Forms NameSpace is not available. – perlfred Jul 06 '23 at 14:21
  • **Thank you for your hints!** I'll see if `ILCreateFromPath(shlobj_core.h)` can help me. This is very easy and then also works in all my other core projects. – perlfred Jul 06 '23 at 14:29
  • Now I forgot the picture when integrating the forms assembly! Oh, that doesn't work in the comments, then I'll try it in the question – perlfred Jul 06 '23 at 14:36
  • Don't add the COM reference, that's the wrong thing. The SDK should add the .NET reference automatically if you have `UseWindowsForms`. But you may need to unload and reload the project. See https://stackoverflow.com/a/72058801/14868997 – Charlieface Jul 06 '23 at 14:48
  • ILCreateFromPath works wonderfully, so the RootDirectory call turns into a 1 line: `IntPtr pidlRoot = GetPIDCLIst(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));` with:`private static IntPtr GetPIDCLIst(string directory){ IntPtr pidl = IntPtr.Zero; // Initialize PIDCList entry try { pidl = Win32API.Shell32.ILCreateFromPath(directory); } finally { Marshal.FreeCoTaskMem(pidl); } return pidl; }` According to the motto: Less is more! Yep, the Forms NameSpace is available with just the project file modification!**Thank you again for the many insights!** – perlfred Jul 06 '23 at 15:38
  • Feel free to answer your own question – Charlieface Jul 06 '23 at 16:00

0 Answers0