C # searches for compliant files within a specified folder

Source: Internet
Author: User

The following is a fuzzy lookup of a file,

The specific function is: Select the folder, search all files hit the SHP layer with the word "_bui" (suffix. shp) and display the information in the ListView. The actual application can be modified casually.

The recursive method is used for deep search, and the shallow search is no longer recorded.

List<filedinfo>listed=new list<fileinfo> ();//storage of files that meet the requirements Filedinfo

String hz= "shp"//File suffix

private int k=0;

Public list<fileinfo>getallfiles (String path)

{

DirectoryInfo dir=new directorinfo (path);

if (!dir. Exists)//To determine whether a folder exists, generally directly select the folder is not a problem, here is the case of manual input

return null;

if (k<1)

{

Fileinfo[]fi=dir. GetFiles ();

for (int i=0;i<fi. length;i++)

{

String Filestr=fi[i]. Name;

if (filestr. Congtains (filestr. LastIndexOf (".") +1)) ==hz)

{

if (filestr. Contains ("_bui") | | Filestr. Contains ("_bui"))

List.add (Fi[i]);

}

}

k++;

}

Else

{

List<fileinfo>sublist=getfilesbydir (path);

foreach (FileInfo subfile in sublist)
{
List.add (Subfile);
}
}
directoryinfo[] SubDir = dir. GetDirectories ();
String spath = "";
foreach (DirectoryInfo d in SubDir)
{
spath = path + "\ \" + d.tostring () + "\ \";
Getallfiles (spath);
}
return List;
}

Private list<fileinfo> Getfilesbydir (string path)
{

DirectoryInfo di = new DirectoryInfo (path);
Locate the file in this directory
Fileinfo[] fi = di. GetFiles ();
Convert fileinfo[] Array to list
list<fileinfo> list = new list<fileinfo> ();

for (int i = 0; I < fi. Length; i++)
{
String filestr = Fi[i]. Name;
if (filestr. Substring (filestr. LastIndexOf (".") + 1)) = = Hz)
{
if (filestr. Contains ("_bui") | | Filestr. Contains ("_bui"))
List.add (Fi[i]);
}
}
return list;
}

C # searches for compliant files within a specified folder

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.