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());
}