The work done in the company is complicated, and there are often some corporate website page files that need to be modified. Some modifications involve all htm page files with the same content, but the number of files reaches several thousand. What if I update it manually? Fortunately, I am a programmer. How can I modify it manually! All write a background management program.
If you modify the files in batches, you need to find all the files in the root directory of the website, including the files in subfolders. The following shows how to obtain the implementation code of all the files in the folder and Its subfolders:
Public class FileAccess
{
// Store all folder names
Private ArrayList dirs;
Public FileAccess ()
{
Dirs = new ArrayList ();
}
// Obtain all file names
Private ArrayList GetFileName (string dirPath)
{
ArrayList list = new ArrayList ();
If (Directory. Exists (dirPath ))
{
List. AddRange (Directory. GetFiles (dirPath ));
}
Return list;
}
// Obtain all folders and subfolders
Private void GetDirs (string dirPath)
{
If (Directory. GetDirectories (dirPath). Length> 0)
{
Foreach (string path in Directory. GetDirectories (dirPath ))
{
Dirs. Add (path );
GetDirs (path );
}
}
}
/// <Summary>
/// Obtain all the file names in the given folder and Its subfolders
/// (The file name is the path and the file name and suffix,
/// When used, the GetAllFileName (). ToArray () method can be converted into an object array.
/// Then, use ToString () to obtain the file name)
/// </Summary>
/// <Param name = "rootPath"> root directory of the folder </param>
/// <Returns> </returns>
Public ArrayList GetAllFileName (string rootPath)
{
Dirs. Add (rootPath );
GetDirs (rootPath );
Object [] allDir = dirs. ToArray ();
ArrayList list = new ArrayList ();
Foreach (object o in allDir)
{
List. AddRange (GetFileName (o. ToString ()));
}
Return list;
}
/// <Summary>
/// If the last method does not know how to use it, call this method.
/// </Summary>
/// <Param name = "rootPath"> </param>
/// <Returns> </returns>
Public List <string> FileName (string rootPath)
{
List <string> list = new List <string> ();
Foreach (object o in GetAllFileName (rootPath). ToArray ())
{
List. Add (o. ToString ());
}
Return list;
}
}