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