Obtain all files in the folder and Its subfolders.

Source: Internet
Author: User

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;
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.