c# - 从转换为 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;
}
});
}
}
解决方案
您的代码不会返回单个路径,它会返回所有打开的 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;
}
推荐阅读
- c++ - 使用 DirectX 11 插入背景颜色?
- r - 如何为每个 x 值制作两个条形图?
- sql - 将自定义语句转换为激活的代码
- c# - POST 到 OData v4 Web API 操作时子集合为空
- r - 使用 R 中的因子 self 的子集重命名因子水平
- android - 试图在 Android Room 中捕获 SQLiteConstraintException 并显示警报
- javascript - 向下传递模块 - javascript
- c++ - 为什么我的代码打印 rvalue 2 次而不是 rvalue & lvalue?
- kotlin - 在 Ktor 中,如何将 InputStream 流式传输到 HttpClient 请求的正文中?
- java - 未找到处理 Intent android.media.action.VIDEO_CAPTURE 的活动(有附加功能)