Folder Subfolder Paths

  • Thread starter Thread starter Gavaskar
  • Start date Start date
G

Gavaskar

Hello

I need to get a listing of all folders and subfolders paths using C# .net
2.0.

such as .\folderA\subfolderB\subfolderC
.\folderA\subfolderD
.\folderA\subfolderD\subfolderE

Any ideas, examples?
 
With .NET, you will have to setup a recursive function, like:

private list<string> GetDirectoryTree(string directory)
{
}

The function will then use a DirectoryInfo and iterate all directories,
feeding them to the same function to see if they have directories. You then
pass the finalized list back out and you can consume it. This need not be a
list<string>, but it is an easy example.

Another option would be to use a process object and call a DOS dir command.
One easy way is to fire off the results to a file, using
C:\directory\filename.txt at the end of the dir command. You can then
consume the results. if you use a /b, it is bare (no attributes) and quite
easy to read.

Hope this helps.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Blog: http://gregorybeamer.spaces.live.com
Twitter: @gbworld

*************************************************
| Think outside the box! |
*************************************************
 
With .NET, you will have to setup a recursive function, like:

private list<string> GetDirectoryTree(string directory)
{
}

The function will then use a DirectoryInfo and iterate all directories,
feeding them to the same function to see if they have directories. You then
pass the finalized list back out and you can consume it. This need not be a
list<string>, but it is an easy example.

Another option would be to use a process object and call a DOS dir command.
One easy way is to fire off the results to a file, using
C:\directory\filename.txt at the end of the dir command. You can then
consume the results. if you use a /b, it is bare (no attributes) and quite
easy to read.

Hope this helps.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Blog: http://gregorybeamer.spaces.live.com
Twitter: @gbworld

*************************************************
| Think outside the box! |
*************************************************
 
Ok, so I've got a CD that has folders and files on that media. I'm creating
a small app that will basically copy from the Cd to the hard drive and
maintain that same folder structure found on the CD. I'm
thinking the easiest way to such as task, is to create a string array with
the folder paths on the CD, then create that folder structure on the hard
drive, then I can copy from source to destination.

If the folder structure on the CD is
..\folder1\subfolder\test1folder\test1_files
.\folder1\subfolder\test2folder\test2_files

.\folder2\subfolder\someothertest\files

My array is fine up till test1folder

filepaths[0] = "\folder1\subfolder\test1folder\" // correct
filepaths[1] = "test2folder" - should be \folder1\subfolder\test2folder

static string filepath;
static string[] filepaths = new string[30];
static int pathcount = 0;

DirectoryInfo dir = new DirectoryInfo("d:\\");

FileSystemInfo[] infos = dir.GetFileSystemInfos();
ListDirectoriesAndFiles(infos);



static void ListDirectoriesAndFiles(FileSystemInfo[] FSInfo)
{
// Iterate through each item.
foreach (FileSystemInfo i in FSInfo)
{
// Check to see if this is a DirectoryInfo object.
if (i is DirectoryInfo)
{

filepath = filepath + @"\"+ i.ToString();
filepaths[pathcount] = filepaths[pathcount] + @"\" +
i.ToString();

// Cast the object to a DirectoryInfo object.
DirectoryInfo dInfo = (DirectoryInfo)i;


// Iterate through all sub-directories.
ListDirectoriesAndFiles(dInfo.GetFileSystemInfos());
}
// Check to see if this is a FileInfo object.
else if (i is FileInfo)
{
// Add one to the file count.
pathcount++;
//filepaths[pathcount] = filepaths[pathcount-1] + @"\" +
i.ToString();
// if subfolders take current path and add it next
element for
// the next subfolder name to added

files++;
break;

}

}



} //end ListDirectoriesAndFiles
 
Ok, so I've got a CD that has folders and files on that media. I'm creating
a small app that will basically copy from the Cd to the hard drive and
maintain that same folder structure found on the CD. I'm
thinking the easiest way to such as task, is to create a string array with
the folder paths on the CD, then create that folder structure on the hard
drive, then I can copy from source to destination.

If the folder structure on the CD is
..\folder1\subfolder\test1folder\test1_files
.\folder1\subfolder\test2folder\test2_files

.\folder2\subfolder\someothertest\files

My array is fine up till test1folder

filepaths[0] = "\folder1\subfolder\test1folder\" // correct
filepaths[1] = "test2folder" - should be \folder1\subfolder\test2folder

static string filepath;
static string[] filepaths = new string[30];
static int pathcount = 0;

DirectoryInfo dir = new DirectoryInfo("d:\\");

FileSystemInfo[] infos = dir.GetFileSystemInfos();
ListDirectoriesAndFiles(infos);



static void ListDirectoriesAndFiles(FileSystemInfo[] FSInfo)
{
// Iterate through each item.
foreach (FileSystemInfo i in FSInfo)
{
// Check to see if this is a DirectoryInfo object.
if (i is DirectoryInfo)
{

filepath = filepath + @"\"+ i.ToString();
filepaths[pathcount] = filepaths[pathcount] + @"\" +
i.ToString();

// Cast the object to a DirectoryInfo object.
DirectoryInfo dInfo = (DirectoryInfo)i;


// Iterate through all sub-directories.
ListDirectoriesAndFiles(dInfo.GetFileSystemInfos());
}
// Check to see if this is a FileInfo object.
else if (i is FileInfo)
{
// Add one to the file count.
pathcount++;
//filepaths[pathcount] = filepaths[pathcount-1] + @"\" +
i.ToString();
// if subfolders take current path and add it next
element for
// the next subfolder name to added

files++;
break;

}

}



} //end ListDirectoriesAndFiles
 
Back
Top