[英]Anonymous function converted to a void returning delegate cannot return a value
[英]Get a return from an anonymous function converted to a void returning delegate
我正在努力返回用戶所關注的當前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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.