-1

I was wondering if there is a way to access the This Pc directory with c#. I can access all my hard drives with the method DriveInfo.GetDrives, but I want to go one level above that. Thanks in advance.

Kg Ro
  • 9
  • 1
  • 9
    It's not really a directory - it's just a view that Windows Explorer shows. – Jon Skeet Jun 06 '22 at 10:14
  • 1
    Look into the [Shell Namespace](https://learn.microsoft.com/en-us/windows/win32/shell/namespace-intro) specifically [Developing with Windows Explorer](https://learn.microsoft.com/en-us/windows/win32/shell/developing-with-windows-explorer). These are COM APIs that unify not just the Windows File System into something that is navigatable via Window Explorer or your own app, but also networks, printers, .NET GAC etc –  Jun 06 '22 at 10:19
  • Alright so to replicate the This Pc view I would just have to show the Special Folders like Desktop ,Documents etc. ? – Kg Ro Jun 06 '22 at 10:19
  • @KgRo no, to replicate that view you have to use the Shell Namespace and APIs. A lot of applications like OneDrive, Google Drive, add extensions that appear under `This PC`. What are you trying to do? – Panagiotis Kanavos Jun 06 '22 at 10:24
  • Which folders appear there can be controlled in the registry, it don't even have to be special folders. Drive grouping display settings are also user controlled. What's displayed there also changes between Windows builds and versions. – Ray Jun 06 '22 at 10:24
  • You can bind to [`MyComputer`](https://learn.microsoft.com/en-us/windows/win32/shell/nse-junction) or lookup the equivalent PIDL to it and then enumerate the contents –  Jun 06 '22 at 10:24
  • @PanagiotisKanavos i was trying to create a very simple File Explorer windows app. But I guess its not as simple as it seems. I will read the resources provided by MickyD and try to figure it out! Thanks for your answers. – Kg Ro Jun 06 '22 at 10:31
  • BTW, [prior to .NET 4, _in-process_ Shell Extensions were **ill-advised**](https://stackoverflow.com/a/2194638/585968) for since the first .NET extension determined the CLR used leading to possible issues for late comers. Even though NET 4 allowed for _side-by-side versions_ of the CLR to be loaded at once thus solving one problem, .NET still insists on using the **horrible** _Runtime Callable Wrappers (RCW)_ that [cause nothing but pain](https://docs.microsoft.com/en-us/windows/win32/shell/shell-and-managed-code) for STA COM and [Office Interop](https://stackoverflow.com/a/56712047/585968) –  Jun 06 '22 at 10:42

1 Answers1

1

No. It's not really a directory, just a view that Windows Explorer has. I'm fairly sure it just enumerates the drives under the hood in a similar fashion to how DriveInfo.GetDrives() does.

ScottishTapWater
  • 3,656
  • 4
  • 38
  • 81
  • 2
    _"...I'm fairly sure it just enumerates the drives under the hood..."_ - kind of, _Explorer_ is a bit like _Visual Studio_ - by themselves neither know anything about reality (the latter requiring a language service). Explorer queries the namespaces (providers/extensions) in the _Windows Shell_ (both are COM-based) hosting various _namespaces_ (providers) so in this case when Explorer or apps navigate/bind to a particular PIDL path, it passes it off to whatever namespace handles that path and it is the provider that carries out the task of say _"just how many files are cluttering up my C:"_. –  Jun 06 '22 at 11:08
  • 1
    …now as to what exactly the file system extension does internally is anyones guess. ;) +1 in any event :) –  Jun 06 '22 at 11:21
  • 1
    You work at MS by any chance @MickyD? Got some quality insights there my dude – ScottishTapWater Jun 06 '22 at 11:53
  • Haha I wish I did. :) I did a lot of native COM programming back in the day including making my own shell namespace extension. Also a Visual Studio language service. –  Jun 06 '22 at 12:46
  • 1
    Ahh that's pretty cool, I've done a bit of pinvoke stuff, but mostly just around manipulating other program's windows, nothing to the same extent as you – ScottishTapWater Jun 06 '22 at 13:41