从匿名函数获取返回值,该返回值转换为无效返回委托

时间:2019-01-13 20:24:12

标签: c# .net delegates anonymous

我正在努力返回用户所关注的当前Active Directory的确切路径,然后我找到了一些代码,尽管它们似乎都无法正常工作且存在错误。但是这段代码似乎起作用了……无论如何,我想返回所提到的路径(在这段代码中称为currDirectory;当我在这段代码中将void类型更改为字符串类型并使用return currDirectory时,我收到错误

  

从匿名函数返回转换为void返回委托无法返回值

任何人都可以更改此代码,以便它可以将currDirectory作为字符串返回吗?

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

1 个答案:

答案 0 :(得分:1)

您的代码将不会返回单个路径,它将返回所有打开的Windows资源管理器实例,因此,如果您打开了多个,则将全部返回,无论如何请看下面我的解决方案,它应该可以解决您的问题。 / p>

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();
    var count = (int)windowsType.InvokeMember("Count", System.Reflection.BindingFlags.GetProperty, null, windows, null);

    string[] currentDirectories = new string[count];
    Parallel.For(0, 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:\", "");

            currentDirectories[i] =  currDirectory;
        }

    });
    return currentDirectories;
}

结果应该是这样的: enter image description here

相关问题