Using system;using System.io;namespace demo{class Program {static string tmppath = @ "D:/lgstest/diretorytes T "; static void Main (string[] args) {//createdirectory (Tmppath); Ergodicdirectory (@ "d:\lgstest\c# practice \shenrulijiec#"); Console.readkey (); }//To determine if the specified path exists, and if it exists, it will be deleted and recreated, otherwise just create the static void CreateDirectory (string path) {//To determine if the given path is stored In, note can only judge path (that is, folder), not specific to the file!!! if (directory.exists (Tmppath)) {DirectoryInfo dic = new DirectoryInfo (Tmppath); Specify whether to delete subdirectories and files, if true, delete this directory, its subdirectories, and all files//If False, the directory is not empty will report an exception, that is, only when the directory is empty can be transmitted false dic. Delete (TRUE); } else {Console.WriteLine (false); }//Create directory Directory.CreateDirectory (Tmppath); }//recursively traverse a directory to output the file name in its subdirectories static void Ergodicdirectory (string path) {IF (directory.exists (path)) {DirectoryInfo dir = new DirectoryInfo (path); if (null! = Dir) {//Returns an array of strongly typed System.IO.FileSystemInfo entries that represent all the files and subdirectories in a directory. filesysteminfo[] FileSystemInfo = dir. Getfilesysteminfos (); for (int i = 0, Imax = filesysteminfo.length; i < IMAX; ++i) {FileSystemInfo Tmpfile = Filesysteminfo[i]; Combined path String tmppath = Path.Combine (path, tmpfile.name); if (Tmpfile is DirectoryInfo) {ergodicdirectory (Tmppath); } else {//output file name Console.WriteLine (Tmpfile.name); }}} or else { Console.WriteLine ("Wwwww"); }} else {Console.WriteLine ("QQQQ"); } } }}
Simple example of file manipulation in C #