List<string>fName = new List();
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "請選擇要打開的文件";
ofd.InitialDirectory = @"C:\Users\XYK\Desktop";
ofd.Multiselect = true;
ofd.Filter = "文本文件|*.txt|所有文件|*.*";
ofd.ShowDialog();
string path = ofd.FileName;
fName.Add(path);
string fileName = Path.GetFileName(path);
listBox1.Items.Add(fileName);
if (path == "")
{
return;
}
using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
{
byte[] buffer = new byte[1024 * 1024 * 10];
int r = fsRead.Read(buffer, 0, buffer.Length);
textBox1.Text=Encoding.Default.GetString(buffer,0,r);
}
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "請選擇保存的路徑";
sfd.Filter = "文本文件|*.txt|所有文件|*.*";
sfd.ShowDialog();
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("保存成功");
string path = fName[listBox1.SelectedIndex];
using(FileStream fsRead=new FileStream(path,FileMode.OpenOrCreate,FileAccess.Read))
{
byte[]buffer=new byte[1024*1024*10];
int r=fsRead.Read(buffer,0,buffer.Length);
textBox1.Text=Encoding.Default.GetString(buffer,0,r);
}