0
votes

Currently I have a PowerShell script that is able to use the Shell.Application COMobject to get the list of programs in my Control Panel. I need to convert this to C# but I can't seem to get it right. Any suggestion on what I'm doing wrong?

The C# code shown below doesn't reutrn any error messages. It just run through (becauses it's null). When i remove the if statment from the C# examples i get "Object reference not set to an instance of an object.". I get that my folder variable is null but i don't get why?

Added the Shell32.Dll(C:\Windows\System32\shell32.dll) reference to my project. Added the Shell32 Using directive

(Working)PowerShell code

$Shell = New-Object -ComObject Shell.Application
$folderName = "::{26EE0668-A00A-44D7-9371-BEB064C98683}\8\" +
 "::{7B81BE6A-CE2B-4676-A29E-EB907A5126C5}");

$folder = $Shell.NameSpace($folderName)

if($folder)
{
    $folder.Items() 
}

(Not Working) C# code

        Type t = Type.GetTypeFromProgID("Shell.Application");
        Shell s = (Shell)Activator.CreateInstance(t);
        var objNameSpace = s.NameSpace(@"::{26EE0668-A00A-44D7-9371-BEB064C98683}\8\::{7B81BE6A-CE2B-4676-A29E-EB907A5126C5}");

        if (objNameSpace != null)
        {
            MessageBox.Show(objNameSpace.Items().Count.ToString());
        }

(More Not Working) C# code. Second Approach

        Shell32.Shell shell = new Shell32.Shell();
        Shell32.Shell objShell = shell.Application;
        var objShellFolder = objShell.NameSpace(@"::{26EE0668-A00A-44D7-9371-BEB064C98683}\8\::{7B81BE6A-CE2B-4676-A29E-EB907A5126C5}");

        if(objShellFolder != null)
        {
            MessageBox.Show(objShellFolder.Items().ToString());
        }
1
Can you clarify on what "not working" means? What error are you getting in both C# examples? - user189198
@TrevorSullivan That would helpful wouldn't it? I get a null refference everytime. "Object reference not set to an instance of an object." This ONLY occurs when i remove the if statment. With the If statment it does nothing at all. -will edit post to reflect this comment. - HiTech
Why would you even want to do this? Querying `HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall` would give you 99% of the applications, wouldn't it? And it's a lot easier to work with - Frode F.
@FrodeF Yes, it will give me every program that writes to the registry. The major problem is an algorthem that will sort through all that junk that can present me ONLY what's in my control panel. There is no unique property that i can filter out that will do this. - HiTech

1 Answers

0
votes

For the Shell32 Interop version of your code, you'll want to use "shell:::{7b81be6a-ce2b-4676-a29e-eb907a5126c5}" for the namespace.

Reference: https://stackoverflow.com/a/48129483/722393