从转换为void返回委托的匿名函数返回

问题描述 投票:0回答:1

我正在努力返回用户关注的当前Active Directory的确切路径然后我找到了一些代码,虽然它们似乎都没有正常工作并且有bug。但是这段代码似乎有用...无论如何我想返回上面提到的路径(在这段代码中称为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# .net delegates anonymous
1个回答
1
投票

您的代码不会返回单个路径,它将返回所有打开的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;
}

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

© www.soinside.com 2019 - 2024. All rights reserved.