Powershell遍历从c#类库返回的list <string>

时间:2017-07-21 22:55:27

标签: c# powershell

我有一个powershell脚本调用c#库(.net框架)并返回一个列表或IEnumerable,我已经尝试过了。

    public class CacheTools
    {
         public static IEnumerable<string> DirSearch(string path)
         {
             var t = GetFiles(path);
             return t;
         }

         static IEnumerable<string> GetFiles(string path)
         {
             Queue<string> queue = new Queue<string>();
             queue.Enqueue(path);
             while (queue.Count > 0)
             {
                 path = queue.Dequeue();
                 try
                 {
                     foreach (string subDir in Directory.GetDirectories     (path))
                     {
                         queue.Enqueue(subDir);
                     }
                 }
                 catch (Exception ex)
                 {
                     Console.Error.WriteLine(ex);
                 }

                 DirectoryInfo source = new DirectoryInfo(path);

                 FileInfo[] files = null;
                 try
                 {
                     files = source.GetFiles();
                 }
                 catch (Exception ex)
                 {
                     Console.Error.WriteLine(ex);
                 }

                 if (files != null)
                 {
                     for (int i = 0; i < files.Length; i++)
                     {
                        yield return files[i].Name;
                     }
                 }
             }
         }
     }

C#

my @keys = $db_obj->SelectAllArrayRef($sql);
print Dumper @keys;

我在迭代返回的列表时遇到问题。

我真的很感激一只手。

Windows 10,powershell 5.1

(编辑) 我遇到的问题是它似乎没有被退回。 $ files什么都没显示。我在visual studio控制台中运行它,我们看到了结果。事实证明,对于我的系统,我必须以管理模式运行它。我没有在powershell ISE中看到错误

0 个答案:

没有答案