Jim_Auricman
March 2nd, 2008, 08:25 AM
I recently downloaded the Visual Studio 2008 for C#. I like the ease of program development. I started off with Console Applications to learn C#. In C/C++ you can get a listing of all the files of a certain type by using the following command:
system("dir/s /b *.txt >text.dat");
I could not find a similar command in C#, so I wrote a recursive function to traverse all the subdirectories of a particular directory and write a list of the files into a text file that match the file extension passed into the function. The FileStream is passed into the function after being created in the 'Main' part of the program and closed there after the function returns. The function works fine as intended, but my question is this, "Is there a less cumbersome method in C# to use which will accomplish what the above mentioned system command in 'C' does?
static void WriteDirInformation(string FullDirPath, FileStream fs, string File_ext)
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@FullDirPath);
string fullPath = Path.GetFullPath(@FullDirPath);
fullPath += "\\";
Console.WriteLine("Files in {0}", FullDirPath);
foreach (System.IO.FileInfo file in dir.GetFiles(File_ext))
{
string fullFilePath = fullPath + file.Name;
Console.WriteLine("{0}", fullFilePath);
fullFilePath += "\n";
byte[] info = new UTF8Encoding(true).GetBytes(fullFilePath);
fs.Write(info, 0, info.Length);
}
DirectoryInfo[] diArr = dir.GetDirectories();
// Display the names of the directories.
if (diArr.Length > 0)
{
foreach (DirectoryInfo dri in diArr)
{
string fullDirPath = fullPath + dri.Name;
Console.WriteLine(fullDirPath);
WriteDirInformation(fullDirPath, fs, File_ext);
}
}
}
system("dir/s /b *.txt >text.dat");
I could not find a similar command in C#, so I wrote a recursive function to traverse all the subdirectories of a particular directory and write a list of the files into a text file that match the file extension passed into the function. The FileStream is passed into the function after being created in the 'Main' part of the program and closed there after the function returns. The function works fine as intended, but my question is this, "Is there a less cumbersome method in C# to use which will accomplish what the above mentioned system command in 'C' does?
static void WriteDirInformation(string FullDirPath, FileStream fs, string File_ext)
{
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@FullDirPath);
string fullPath = Path.GetFullPath(@FullDirPath);
fullPath += "\\";
Console.WriteLine("Files in {0}", FullDirPath);
foreach (System.IO.FileInfo file in dir.GetFiles(File_ext))
{
string fullFilePath = fullPath + file.Name;
Console.WriteLine("{0}", fullFilePath);
fullFilePath += "\n";
byte[] info = new UTF8Encoding(true).GetBytes(fullFilePath);
fs.Write(info, 0, info.Length);
}
DirectoryInfo[] diArr = dir.GetDirectories();
// Display the names of the directories.
if (diArr.Length > 0)
{
foreach (DirectoryInfo dri in diArr)
{
string fullDirPath = fullPath + dri.Name;
Console.WriteLine(fullDirPath);
WriteDirInformation(fullDirPath, fs, File_ext);
}
}
}