Read all files in a folder and read text data in the file

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Using system. IO;
Using system. Text. regularexpressions;

Namespace readfiles
{
Public partial class formnode: Form
{
Public formnode ()
{
Initializecomponent ();
}

private void btnsubmit_click (Object sender, eventargs e)
{< br> If (txtnode. text. tostring () = "" | txtnode. text. tostring () = NULL | txtnode. text. tostring () = string. empty)
{< br> MessageBox. show ("Enter the measurement point to be queried! "," Non-empty measurement point warning! ");
return;
}

Else if (txtpath. text. tostring () = "" | txtpath. text. tostring () = NULL | txtpath. text. tostring () = string. empty)
{
MessageBox. Show ("Enter the folder path! "," Warning about path not empty! ");
Return;
}

Else if (txtnode. Text = "'" | txtnode. Text = "'")
{
MessageBox. Show ("contains invalid characters. Please try again! "," Illegal character warning! ");
}

Else
{
String Path = txtpath. Text. tostring () + "\\";
Findfile (PATH );
}

# Region test
// Int COUNT = 0;
// Filestream FS = new filestream ("", filemode. Open, fileaccess. Read );
// Streamreader sr = new streamreader (FS, encoding. getencoding ("gb2312 "));
// String line = Sr. Readline ();
// For (INT I = 0; line! = NULL; I ++)
//{
// If (line. Contains ("8002") = true)
//{
// Count ++;
// Lisinfo. Items. Add (line );
//}
// Line = Sr. Readline ();
//}
// Sr. Close ();
// Fs. Close ();
# Endregion
}

Public void findfile (string dirpath) // The dirpath parameter is the specified directory.
{
// Search for files in the specified directory and subdirectory, and list subdirectories and files in listbox1
String filename = "";
Directoryinfo dir = new directoryinfo (dirpath );
Try
{
Foreach (directoryinfo D in Dir. getdirectories () // find the subdirectory
{
Findfile (DIR + D. tostring () + "\\");
Lisinfo. Items. Add (DIR + D. tostring () + "\"); // Add the directory name in listbox1
}
Foreach (fileinfo F in Dir. getfiles ("*. dat") // find the file
{
// Lisinfo. Items. Add (DIR + F. tostring (); // Add the file name in listbox1
// Lisinfo. Items. Add (F. tostring (); // Add the file name in listbox1
Filename = f. tostring ();
Int COUNT = 0;
Filestream FS = new filestream (txtpath. Text + "\" + filename, filemode. Open, fileaccess. Read );
Streamreader sr = new streamreader (FS, encoding. getencoding ("gb2312 "));
String line = Sr. Readline ();
For (INT I = 0; line! = NULL; I ++)
{
If (line. Contains (txtnode. Text. tostring () = true)
{
Count ++;
Lisinfo. Items. Add (line );
}
Line = Sr. Readline ();
}
Sr. Close ();
FS. Close ();
}
}
Catch (exception E)
{
MessageBox. Show (E. message, "error capture prompt! ");
}
}

///


// open the data loading window
///
///
//
private void btnload_click (Object sender, eventargs e)
{< br> loadfiles LD = new loadfiles ();
lD. showdialog ();
}

private void btnsound_click (Object sender, eventargs e)
{< br> sound so = new sound ();
SO. showdialog ();
}< BR >}

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.