Operation of the file

Source: Internet
Author: User

Directory

1.              list<string> Files=  directory.getfiles (Fpath). Where (File=>file. EndsWith (".txt")). ToList ();
string fpath = Folderbrowserdialog1.selectedpath;
string[] files1 = Directory.GetFiles (Fpath);//directory.getfiles (Fpath, "*.txt");
2.varFilteredfiles =Directory. GetFiles (Path,"*.*") . Where (File= = file. ToLower (). EndsWith ("aspx") || File. ToLower (). EndsWith ("ascx")) . ToList ();3. GetFiles can only match a single pattern, but you LINQ calls GetFiles's multi-mode: fileinfo[] fi=New string[]{"*.txt","*.doc"} . SelectMany (i=di. GetFiles (i, searchoption.alldirectories)). Distinct (). ToArray (); see here:4I'm afraid you'll do the property after this, and I'll mutate the regular expression from here. varSearchpattern =NewRegex (@"$ (? <=\. ( Aspx|ascx))", regexoptions.ignorecase);varFiles = directory.getfiles (path). Where (f =Searchpattern.ismatch (f));5. I'll try to like specifyingvarSearchpattern ="as?x"; it should work. 6. I have the following:varext =New string[] {". ASPX",". ASCX" }; Fileinfo[] Collection= ( fromFiinch NewDirectoryInfo (path). GetFiles ()whereext. Contains (FI. Extension.toupper ())SelectFI). ToArray (); edit: Correct the mismatch between the directory and the DirectoryInfo7. /// <summary> ///Returns The names of files in a specified directories that match the specified patterns using LINQ/// </summary> /// <param name= "Srcdirs" >The directories to seach</param> /// <param name= "Searchpatterns" >The list of search patterns</param> /// <param name= "SearchOption" ></param> /// <returns>The list of files that match the specified pattern</returns>  Public Static string[] Getfilesusinglinq (string[] srcdirs,string[] searchpatterns, SearchOption searchoption=searchoption.alldirectories) {varR = fromDirinchSrcdirs fromSearchpatterninchSearchpatterns fromFinchDirectory.GetFiles (dir, searchpattern, searchoption)SelectF; returnR.toarray ();}8.  Public Static BOOLCheckfiles (stringPatha,stringPATHB) {  string[] Extantionformat =New string[] {". War",". Pkg" }; returncheckfiles (Patha, PATHB, Extantionformat);}  Public Static BOOLCheckfiles (stringPatha,stringPATHB,string[] extantionformat) {System.IO.DirectoryInfo Dir1=NewSystem.IO.DirectoryInfo (Patha); System.IO.DirectoryInfo Dir2=NewSystem.IO.DirectoryInfo (PATHB); //Take a snapshot of the file system. LIST1/2 would contain only WAR or PKG//Files//Fileinfosa would contain all of the files under Path directoriesfileinfo[] Fileinfosa = Dir1. GetFiles ("*.*", System.IO.SearchOption.AllDirectories); //The list would contain all of the files that are . Extantion[]//Run on all extantion in extantion array and compare them by lower case to//The file item extantion ...List<system.io.fileinfo> List1 = ( fromExtiteminchExtantionformat fromFileiteminchFileinfosawhereExtitem.tolower (). Equals (FileItem.Extension.ToLower ())SelectFileitem).  ToList (); //Take a snapshot of the file system. LIST1/2 would contain only WAR or//PKG Files//Fileinfosa would contain all of the files under Path directoriesfileinfo[] FILEINFOSB = Dir2. GetFiles ("*.*", System.IO.SearchOption.AllDirectories); //The list would contain all of the files that are . Extantion[]//Run on all extantion in extantion array and compare them by lower case to//The file item extantion ...List<system.io.fileinfo> List2 = ( fromExtiteminchExtantionformat fromFileiteminchFILEINFOSBwhereExtitem.tolower (). Equals (FileItem.Extension.ToLower ())SelectFileitem).  ToList (); Filecompare Myfilecompare=NewFilecompare (); //This query determines whether the folders contain//identical file lists, based on the custom file comparer//That's defined in the Filecompare class.  returnList1. SequenceEqual (List2, Myfilecompare); }9.varFilteredfiles =Directory. Enumeratefiles (Path,"*.*")// . NET4 better than ' GetFiles ' . Where (//ignorecase faster than tolower ...File = file. ToLower (). EndsWith ("aspx")  || File. EndsWith ("ascx", StringComparison.OrdinalIgnoreCase)) . ToList (); Don't forget the new. Net4directory.enumeratefiles for performance gains (what is the difference between directories). With Directory.GetFiles? "IGNORECASE" should be faster than "TOLOWER" or, it may be faster splitting glob (at least it looks cleaner):"*.ext1;*.ext2". Split (';') . SelectMany (g=directory.enumeratefiles (Path, g)). ToList ();Ten. A demo like this:voidMain () {foreach(varFinchGetfilestoprocess ("c:\\",New[] {". XML",". txt"})) Debug.WriteLine (f);}Private Staticienumerable<string> getfilestoprocess (stringPath, ienumerable<string>extensions) { returnDirectory.GetFiles (Path,"*.*")  . Where (f=extensions. Contains (Path.getextension (f). ToLower ()));}

Operation of the file

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.