C #: On-line read/write of files and use of File directory dialog boxes
Source: Internet
Author: User
dialog box to read and write files by line
To determine if a file exists: File.exists (string FilePath)
Determine if the directory exists: directory. Exists ("D:\\lastestversion")
To read a file by row:
int filecount=0;
Open the file just specified such that no one else can use it.
StreamReader sr = new StreamReader (TextBox1.Text.Trim ());
while (Sr. Peek () >-1)//streamreader.peek () returns the next available character, but does not use it
{
ListBox1.Items.Add (Sr. ReadLine ());
filecount++;
}
Sr. Close ();
To write a file by line:
StreamWriter sw = new StreamWriter ("D:\\result.txt");
for (int i=0;i<10;i++)
{
Sw. WriteLine ("This is the first" +i.tostring () + "row data");
}
Use of the File Directory dialog box
The file dialog box is the use of filter conditions:
String Resultfile= "";
OpenFileDialog openFileDialog1 = new OpenFileDialog ();
Openfiledialog1.initialdirectory = "D:\\patch";
Openfiledialog1.filter = "All Files (*.*) |*.*|txt files (*.txt) |*.txt";
Openfiledialog1.filterindex = 2;
Openfiledialog1.restoredirectory = true;
if (openfiledialog1.showdialog () = = DialogResult.OK)
Resultfile=openfiledialog1.filename;
Use of the Directory dialog box:
String Resultfolder= "";
FolderBrowserDialog openfolderdialog1=new FolderBrowserDialog ();
Openfolderdialog1.rootfolder=environment.specialfolder.mycomputer;
if (Openfolderdialog1.showdialog () ==dialogresult.ok)
Resultfolder=openfolderdialog1.selectedpath;
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.