Common methods for C # file streams

Source: Internet
Author: User

Open File Select Picture

private void Btnchoseimage_click (object sender, EventArgs e)
{
OpenFileDialog Objfiledialog = new OpenFileDialog ();
DialogResult result= Objfiledialog.showdialog ();
if (Result==dialogresult.ok)
{
This.pbStu.Image = Image.FromFile (objfiledialog.filename);
}
}

Write file
private void Btnwriteall_click (object sender, EventArgs e)
{
"1" Create a file stream
FileStream fs = new FileStream ("C:\\myfile.txt", FileMode.Create);

"2" Create writer
StreamWriter SW = new StreamWriter (Fs,encoding.default);

"3" writes data in a stream way
Sw. Write (This.txtContent.Text.Trim ());

"4" closes the writer
Sw. Close ();

"5" closes the file stream
Fs. Close ();
}
Read file
private void Btnreadall_click (object sender, EventArgs e)
{
"1" Create a file stream
FileStream fs = new FileStream ("C:\\myfile.txt", FileMode.Open);

"2" Create reader
StreamReader sr = new StreamReader (FS, Encoding.default);

"3" reads data in a stream
This.txtContent.Text = Sr. ReadToEnd ();

"4" Closes the reader
Sr. Close ();

"5" closes the file stream
Fs. Close ();
}
Simulating write system logs
private void Btnwriteline_click (object sender, EventArgs e)
{
"1" creates a file stream (file mode: Append)
FileStream fs = new FileStream ("C:\\myfile.txt", filemode.append);

"2" Create writer
StreamWriter SW = new StreamWriter (FS);

"3" writes data "line by row" in a stream
Sw. WriteLine (DateTime.Now.ToString () + "File operation OK! ");

"4" closes the writer
Sw. Close ();

"5" closes the file stream
Fs. Close ();
}
deleting files
private void Btndel_click (object sender, EventArgs e)
{
File.delete (This.txtFrom.Text.Trim ());
}
Copying files
private void Btncopy_click (object sender, EventArgs e)
{
if (File.exists (This.txtTo.Text.Trim ()))//First determine if the file exists (if the file exists, direct copy will cause an error)
{
File.delete (This.txtTo.Text.Trim ());//delete files
}
File.Copy (This.txtFrom.Text.Trim (), This.txtTo.Text.Trim ()); Copying files
}
Moving files
private void Btnremove_click (object sender, EventArgs e)
{
First determine if the destination path file exists (if the file exists, the direct copy will be error)
if (File.exists (This.txtTo.Text.Trim ()))
{
File.delete (This.txtTo.Text.Trim ());//delete files
}
if (File.exists (This.txtFrom.Text.Trim ()))//If the current file exists then move
{
Moving files
File.move (This.txtFrom.Text.Trim (), This.txtTo.Text.Trim ());
}
Else
{
MessageBox.Show ("file does not exist!) ");
}
}
Get the files in the current directory
private void Btnshowallfiles_click (object sender, EventArgs e)
{
string[] files = Directory.GetFiles ("C:\\myfiles");
This.txtContent.Clear ();
foreach (string item in files)
{
This.txtContent.Text + = Item + "\ r \ n";
}
}
Get all subdirectories under the specified directory
private void Btnshowsubdir_click (object sender, EventArgs e)
{
string[] dirs = directory.getdirectories ("C:\\drivers");
This.txtContent.Clear ();
foreach (string item in dirs)
{
This.txtContent.Text + = Item + "\ r \ n";
}
}

Create a Directory
private void Btncreate_click (object sender, EventArgs e)
{
Directory.CreateDirectory ("C:\\myfiles\\newfiles");
}

Delete all subdirectories and files under the specified directory
private void Btndelallfiles_click (object sender, EventArgs e)
{
Directory.delete ("C:\\myfiles");//require directory to be empty

You can delete a directory that is not empty by using the DirectoryInfo object
DirectoryInfo dir = new DirectoryInfo ("C:\\myfiles");
Dir. Delete (TRUE);
}

Common methods for C # file streams

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.