I am working on returning the exact path of the current Active Directory which user has focused on then I found some code, although none of them seems to work correctly and have bugs. But this code seems to work... anyway I want to return the mentioned path (which is called currDirectory
in this code; when I change the void type to string type in this code and use return currDirectory
, I get an error
Return from an anonymous function converted to a void returning delegate cannot return a value
Can anyone change this code so that it can return the currDirectory
as a string?
class Class2
{
public static void Main()
{
RefreshWindow();
}
public static string RefreshWindow()
{
Guid CLSID_ShellApplication = new Guid("13709620-C279-11CE-A49E-444553540000");
Type shellApplicationType = Type.GetTypeFromCLSID(CLSID_ShellApplication, true);
object shellApplication = Activator.CreateInstance(shellApplicationType);
object windows = shellApplicationType.InvokeMember("Windows", System.Reflection.BindingFlags.InvokeMethod, null, shellApplication, new object[] { });
Type windowsType = windows.GetType();
object count = windowsType.InvokeMember("Count", System.Reflection.BindingFlags.GetProperty, null, windows, null);
Parallel.For(0, (int)count, i =>
{
object item = windowsType.InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, null, windows, new object[] { i });
Type itemType = item.GetType();
string itemName = (string)itemType.InvokeMember("Name", System.Reflection.BindingFlags.GetProperty, null, item, null);
if (itemName == "Windows Explorer" || itemName == "File Explorer")
{
string currDirectory = HttpUtility.HtmlEncode((string)itemType.InvokeMember("LocationURL", System.Reflection.BindingFlags.GetProperty, null, item, null)).Replace("///", @"\").Replace("/", @"\").Replace("%20", " ").Replace(@"file:\", "");
Console.WriteLine(currDirectory);
Console.Read();
return currDirectory;
}
});
}
}