C # Open TXT file content and save content to TXT file

Source: Internet
Author: User

OpenFileDialog ofd = new OpenFileDialog ();
Ofd. Title = "Please select a text file to open";
Ofd. InitialDirectory = @ "C:\Users\SpringRain\Desktop";
Ofd. MultiSelect = true;
Ofd. Filter = "text file |*.txt| all files |*.*";
Ofd. ShowDialog ();

Get the path to the file selected by the user
String path = Ofd. FileName;
To store the full path of a file in a generic collection
List. ADD (path);
Get the file name of the user open
String fileName = Path.getfilename (Path);
Place the file name in the listbox
LISTBOX1.ITEMS.ADD (FileName);
if (Path = = "")
{
Return
}
using (FileStream fsread = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] buffer = new BYTE[1024 * 1024 * 5];
int r = fsread.read (buffer, 0, buffer. Length);
TextBox1.Text = Encoding.Default.GetString (buffer, 0, R);
}

SaveFileDialog sfd = new SaveFileDialog ();
SfD. InitialDirectory = @ "C:\Users\SpringRain\Desktop";
SfD. Title = "Please select the file path to save";
SfD. Filter = "text file |*.txt| all files |*.*";
SfD. ShowDialog ();

Get the path to the file that the user wants to save
String path = sfd. FileName;
if (Path = = "")
{
Return
}
using (FileStream fswrite = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = Encoding.Default.GetBytes (TextBox1.Text);
Fswrite.write (buffer, 0, buffer.) Length);
}
MessageBox.Show ("saved successfully");

To get the full path for the double-click File
String path = List[listbox1.selectedindex];
using (FileStream fsread = new FileStream (path, FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] buffer = new BYTE[1024 * 1024 * 5];
int r = fsread.read (buffer, 0, buffer. Length);

TextBox1.Text = Encoding.Default.GetString (buffer, 0, R);
}

C # Open TXT file content and save content to TXT file

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.