0

How to get all source safe sub folders using c# code? We want to get all sub folders and folders in sub folders as well. For example, TestProject has 2 folders, folder a and b. and a has sub folder a1.

Extract all paths: 1. TestProject -> a 2. TestProject -> a -> a1 3. TestProject -> b

2 Answers2

0

If you want to list all the sub directories which contains only files then you can use this

   Public static IEnumerable<string>   GetSubdirectoriesContainingOnlyFiles(string path)
         {
             return from subdirectory in Directory.GetDirectories(path, "*", SearchOption.AllDirectories)
                    where Directory.GetDirectories(subdirectory).Length == 0
                    select subdirectory;
        }

but if you want just want to parse through all the directories & sub directories then you can use this

   static void Main(string[] args)
    {
      DirSearch(@"c:\temp");
      Console.ReadKey();
    }

  static void DirSearch(string dir)
   {
     try
      {
       foreach (string f in Directory.GetFiles(dir))
        Console.WriteLine(f);
       foreach (string d in Directory.GetDirectories(dir))
        {
          Console.WriteLine(d);
          DirSearch(d);
        }

    }
   catch (System.Exception ex)
    {
      Console.WriteLine(ex.Message);
    }
  }
SoftwareNerd
  • 1,875
  • 8
  • 29
  • 58
0

I got this working like this

using System;
using System.Collections.ObjectModel;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

using System.IO;

namespace TFS_Path_Extraction
{
    class Program
    {
        static void Main(string[] args)
        {
            TeamFoundationServer server = new TeamFoundationServer("<TFS path of folders you want>");
            VersionControlServer version = server.GetService(typeof(VersionControlServer)) as VersionControlServer;

            ItemSet items = version.GetItems(@"$\", RecursionType.Full);
            foreach (Item item in items.Items)
            {
                if (item.ItemType == ItemType.Folder)
                {
                    System.Console.WriteLine(item.ServerItem);
                }
            }
            Console.Read();
        }
    }
}