דניאל רשת
דניאל רשת

Reputation: 109

How to convert List<Tuple<string, int>> to string[]?

The method

static List<Tuple<string, int>> F(string root, int rootLevel)
{
    List<Tuple<string, int>> result = new List<Tuple<string, int>>();

    foreach (var item in Directory.EnumerateDirectories(root))
    {
        try
        {
            result.Add(new Tuple<string, int>(item, rootLevel + 1));

            result.AddRange(F(item, rootLevel + 1));
        }
        catch (UnauthorizedAccessException ex)
        {
            string tried = "";
        }

    }

    return result;

}

Then in the constructor i'm using it

var dirs = F(textBox3.Text, 0);
var deep = (from d in dirs
            orderby d.Item2 descending
            select d).FirstOrDefault().Item2;

Search_Engine se = new Search_Engine();
se.Run();

The problem in this case is that se.Run(); should return string[] . And dirs is List<Tuple<string, int>>

Upvotes: 1

Views: 571

Answers (2)

M. Pipal
M. Pipal

Reputation: 734

You can do it easily on your own, like this:

var length = dirs.Count();
string[] array = new string[length];
for(int i = 0; i < length; i++)
{
  array[i] = dirs[i].Item1;
}
//array is now what you want

Upvotes: 0

Gilad Green
Gilad Green

Reputation: 37281

You can do this:

var dirs = F(textBox3.Text, 0);

Search_Engine se = new Search_Engine();
se.Run(dirs.Select(item => item.Item1).ToArray());

Upvotes: 4

Related Questions