I did not know the names of these classes and methods, but I used pseudocode and ideas.
Alas, when books are used, they hate less. It seems that the old saying still makes sense.
Using system. IO;
Public static string pathdir = string. empty;
Public static string savepath = string. empty;
Public static stringbuilder sb = new stringbuilder ();
Static void main (string [] ARGs)
{
Pathdir = @ "D: \ Wes 2009 related materials \";
Savepath = @ "D: \ filepath.txt ";
Getdirectories (pathdir );
// Findfile (pathdir );
Console. Readline ();
}
/// <Summary>
/// Obtain the local folder and all files
/// </Summary>
/// <Param name = "path"> path </param>
Private Static void getdirectories (string path)
{
Try
{
String [] filenames = system. Io. Directory. getfiles (PATH );
String [] directories = system. Io. Directory. getdirectories (PATH );
Foreach (string dir in directories)
{
Console. writeline ("directoriesname: {0}", DIR );
SB. append (DIR + "\" + "\ r \ n ");
Getdirectories (DIR + "\\");
}
Foreach (string file in filenames)
{
Console. writeline ("filename: {0}", file );
SB. append (File + "\ r \ n ");
}
Writetext (savepath, SB. tostring ());
Console. writeline ("all directories and files have been written into filepath.txt successfully! ");
}
Catch (exception)
{
Throw;
}
}
/// <Summary>
/// Recursively find all subdirectories and files in the directory
/// </Summary>
/// <Param name = "dir"> specified directory </param>
Public static void findfile (string DIR)
{
// Search for objects in the specified directory and subdirectory
Directoryinfo dir = new directoryinfo (DIR );
Try
{
Foreach (directoryinfo D in Dir. getdirectories () // find the subdirectory
{
SB. append (DIR + D. tostring () + "\" + "\ r \ n ");
Findfile (DIR + D. tostring () + "\\");
}
Foreach (fileinfo F in Dir. getfiles ("*. *") // find the file
{
SB. append (DIR + F. tostring () + "\ r \ n ");
}
Writetext (savepath, SB. tostring ());
Console. writeline ("all directories and files have been written into filepath.txt successfully! ");
}
Catch (exception)
{
Throw new exception ();
}
}
/// <Summary>
/// Write a file
/// </Summary>
/// <Param name = "savepath"> Save path </param>
/// <Param name = "datainfo"> data info </param>
Public static void writetext (string savepath, string datainfo)
{
Try
{
If (! File. exists (savepath ))
{
System. Io. file. Create (savepath );
}
// Pass the filepath and filename to the streamwriter Constructor
Streamwriter Sw = new streamwriter (savepath );
Sw. writeline (datainfo); // write a line of text
Sw. Close (); // close the file
}
Catch (exception)
{
Throw;
}
}
/// <Summary>
/// Retrieve the file name in the local directory
/// </Summary>
/// <Param name = "strpath"> </param>
Private void showdirfiles (string strpath)
{
Try
{
If (! Directory. exists (strpath ))
{// The directory does not exist. Create a directory.
Directory. createdirectory (strpath );
}
Directoryinfo mydir = new directoryinfo (strpath );
Fileinfo [] files = mydir. getfiles ();
This. listboxc. Items. Clear (); // clear the existing project in ListBox
For (INT I = 0; I <files. length; I ++)
{
This. listboxc. Items. Add (files [I]. tostring ());
}
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}
}
Public void listfiles (filesysteminfo info)
{
If (! Info. exists) return;
Directoryinfo dir = info as directoryinfo;
// Not a directory
If (DIR = NULL) return;
Filesysteminfo [] files = dir. getfilesysteminfos ();
For (INT I = 0; I <files. length; I ++)
{
Fileinfo file = files [I] As fileinfo;
// File
If (file! = NULL)
{
// Console. writeline (file. fullname + "\ t" + file. Length );
If (file. fullname. substring (file. fullname. lastindexof (".") = ". jpg ")
// JPG format is displayed here. If it is not added, all files in the format can be traversed.
{
This. list1.items. Add (File );
// MessageBox. Show (file. fullname. substring (file. fullname. lastindexof (".")));
}
}
// Recursively call sub-Directories
Else
{
Listfiles (files [I]);
}
}
}
}