As the topic says, i'm trying to intergrate these code from Programatically Pin\UnPin the folder from quick access menu in windows 10 into a .net framework dll.
This is my code and it will throw error "Common Language Runtime detected an invalid program."
private void PinFolderToQuickAccess(string path)
{
try
{
using (var runspace = RunspaceFactory.CreateRunspace())
{
runspace.Open();
var ps = PowerShell.Create();
var shellApplication =
ps.AddCommand("New-Object").AddParameter("ComObject", "shell.application").Invoke();
dynamic nameSpace = shellApplication.FirstOrDefault()?.Methods["NameSpace"].Invoke(path);
nameSpace?.Self.InvokeVerb("pintohome");
}
}
catch (Exception e)
{
Console.Error.WriteLine("Err: {0}", e);
}
}
private void UnpinFolderFromQuickAccess(string path)
{
try
{
using (var runspace = RunspaceFactory.CreateRunspace())
{
runspace.Open();
var ps = PowerShell.Create();
var removeScript =
$"((New-Object -ComObject shell.application).Namespace(\"shell:::{{679f85cb-0220-4080-b29b-5540cc05aab6}}\").Items() | Where-Object {{ $_.Path -EQ \"{path}\" }}).InvokeVerb(\"unpinfromhome\")";
ps.AddScript(removeScript);
ps.Invoke();
}
}
catch (Exception e)
{
Console.Error.WriteLine("Err: {0}", e);
}
}
[TestMethod]
public void RemoveFromQuickAccess_WithAddFirst()
{
QuickAccessHandler handler = new QuickAccessHandler();
string testPath = @"C:\Users\hp\Downloads\Compressed";
handler.RemoveFromQuickAccess(testPath);
}
I have tried another solution from here Is it possible programmatically add folders to the Windows 10 Quick Access panel in the explorer window?
but this time it will stuck at InvokeVerb("pintohome")
private void PinFolderToQuickAccess(string path)
{
try
{
Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
Object shell = Activator.CreateInstance(shellAppType);
Shell32.Folder2 f = (Shell32.Folder2)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { path });
f.Self.InvokeVerb("pintohome");
}
catch (Exception e)
{
Console.Error.WriteLine("Err: {0}", e);
}
}
private void UnpinFolderFromQuickAccess(string path)
{
try
{
Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
Object shell = Activator.CreateInstance(shellAppType);
Shell32.Folder2 f2 = (Shell32.Folder2)shellAppType.InvokeMember("NameSpace", System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { "shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}" });
foreach (FolderItem fi in f2.Items())
{
if (fi.Path == path)
{
((FolderItem)fi).InvokeVerb("unpinfromhome");
}
}
}
catch (Exception e)
{
Console.Error.WriteLine("Err: {0}", e);
}
}
Is it possible to use a powershell instance inside a .net framework dll? Or how should it be?