C gets a simple instance of all directories and files
- 2020-05-19 05:37:48
- OfStack
public class FileAccess
{
// Store all folder names
private ArrayList dirs;
public FileAccess()
{
dirs = new ArrayList();
}
// Get all the file names
private ArrayList GetFileName(string dirPath)
{
ArrayList list = new ArrayList();
if (Directory.Exists(dirPath))
{
list.AddRange(Directory.GetFiles(dirPath));
}
return list;
}
// Gets 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>
/// Gets all file names under the given folder and its subfolders
/// (file name path plus file name and suffix ,
/// When you use it GetAllFileName().ToArray() Method can be converted to object An array of
/// then ToString() Get file names respectively)
/// </summary>
/// <param name="rootPath"> Folder root </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 you didn't know how to use the last method, why don't you 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;
}
}