首页 > 解决方案 > 从转换为 void 返回委托的匿名函数获取返回

问题描述

我正在努力返回用户关注的当前 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;
            }
        });
    }
}

标签: c#.netdelegatesanonymous

解决方案


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

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

结果应该是这样的: 在此处输入图像描述


推荐阅读