C #. NET gets all the files under the folder (multi-layered recursion), and gets the interval data (Jsion,xml and other data) written out to the processing file, learning to share ~

Source: Internet
Author: User

static void Main(string[] args)
{

StringPath= String. Format (@ "c:\users\administrator\desktop\{0}", @ "Shenzheng");//root directory
List<string> filepaths =getalldirectories(path);//Returns a collection of all files (including paths) under the root directory
foreach (var item in filepaths)
{
StreamReader read = new StreamReader (item, ENCODING.UTF8);//Get file stream based on file path
String fileName = Path.getfilenamewithoutextension (item);//File name
String FilePath = Path.getdirectoryname (item);//folder where the files are located
if (!read. Endofstream)//Whether read completed
{
string aline = read. ReadToEnd ();//Read all------//string aline = read. ReadLine (); read one line
Console.WriteLine (read. ReadLine ());
String Keystar = "* * *";//keyword start
String ketend = "&&&";//keyword end//get string between
int star = Aline. IndexOf (Keystar) + keystar.length;//plus the number of bits that the Keystar occupies
int end = Aline. IndexOf (Ketend);
if (Star > 0 && end > Star)//keyword not found
{
String newstr = Aline. Substring (Star, End-star);
Console.WriteLine (NEWSTR);//Print to the console to see if the desired information is intercepted
Console.WriteLine (FileName + "Print Complete");
Char[] dd = new char[] {'] ', ', '};
string[] STRs = newstr. Split (new string[] {"Split String"}, Stringsplitoptions.removeemptyentries);//Divide the string you want by the rule to get a string collection
StreamWriter SW = new StreamWriter (@ "C:\Users\Administrator\Desktop\out\" + fileName + "process. txt", false);//Output directory and file name definition
for (int i = 0; i < STRs. Count (); i++)
{
Console.WriteLine (Strs[i] + "," + (i + 1). ToString ());//Print to console
Sw. WriteLine (Strs[i] + "," + (i + 1). ToString ());//write to file
Sw. Flush ();//Empty StreamWriter cache, otherwise it will not be fully written
Thread.Sleep (20);

}
Console.WriteLine (FileName + "write complete");
Console.readkey ();
}
Else
{
Console.WriteLine ("Current Document" + FileName + "No data conforming to the rules, skip!") ");
}
}
}

static list<string> fileslist = new list<string> ();
public static list<string>getalldirectories(String RootPath)
{
string[] subpaths = directory.getdirectories (RootPath);//Get all subdirectories

foreach (string path in subpaths)
{
GetAllDirectories (path);//Do the same thing as the root directory for each word directory: The subdirectory is found and the file name of the current directory is stored in the list
}
string[] files = directory.getfiles (RootPath);
foreach (string file in files)
{
Fileslist.add (file);//The full file name in the current directory is saved to the file list
}
return fileslist;
}

}

C #. NET gets all the files under the folder (multi-layered recursion), and gets the interval data (Jsion,xml and other data) written out to the processing file, learning to share ~

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.