打开对话框:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;namespace _117_Dialog打开对话框{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){//点击弹出对话框OpenFileDialog ofd = new OpenFileDialog();//设置对话框标题ofd.Title = "选择你要打开的文件O(∩_∩)O";//设置对话框可以多选ofd.Multiselect = true;//设置对话框的初始目录ofd.InitialDirectory = @"C:\Users\46124\Desktop\.wav音乐";//设置对话框可筛选的文件类型的内容ofd.Filter = "媒体文件|*.wav|文本文件|*.txt|所有文件|*.*";//展示对话框ofd.ShowDialog();//获得在打开对话框中选中的路径string path = ofd.FileName;//如果路径为空,则立即跳出循环if (path==""){return;}using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)){byte[] buffer = new byte[1024 * 1024 * 5];//实际读取到的字节数int readbyte = fsRead.Read(buffer, 0, buffer.Length);textBox1.Text = Encoding.UTF8.GetString(buffer, 0, readbyte);}}}}
打开和保存文件(目前只实现文本文件)
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;namespace _117_Dialog打开对话框{public partial class Form1 : Form{public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){//点击弹出对话框OpenFileDialog ofd = new OpenFileDialog();//设置对话框标题ofd.Title = "选择你要打开的文件O(∩_∩)O";//设置对话框可以多选ofd.Multiselect = true;//设置对话框的初始目录ofd.InitialDirectory = @"C:\Users\46124\Desktop\.wav音乐";//设置对话框可筛选的文件类型的内容ofd.Filter = "文本文件|*.txt|媒体文件|*.wav|所有文件|*.*";//展示对话框ofd.ShowDialog();//获得在打开对话框中选中的路径string path = ofd.FileName;//如果路径为空,则立即跳出循环if (path == ""){return;}using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)){byte[] buffer = new byte[1024 * 1024 * 5];//实际读取到的字节数int readbyte = fsRead.Read(buffer, 0, buffer.Length);textBox1.Text = Encoding.Default.GetString(buffer, 0, readbyte);}MessageBox.Show("打开成功");}private void btnSave_Click(object sender, EventArgs e){SaveFileDialog sfd = new SaveFileDialog();sfd.Title = "请选择要保存的路径";sfd.InitialDirectory = @"C:\Users\46124\Desktop";sfd.Filter = "文本文件|*.txt|媒体文件|*.wav|所有文件|*.*";//获取保存文件的路径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("保存成功");}}}
添加了前景背景色,字体
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.IO;namespace _117_Dialog打开对话框{public partial class Form1 : Form{public Form1(){InitializeComponent();}/// <summary>/// 打开对话框/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button1_Click(object sender, EventArgs e){//点击弹出对话框OpenFileDialog ofd = new OpenFileDialog();//设置对话框标题ofd.Title = "选择你要打开的文件O(∩_∩)O";//设置对话框可以多选ofd.Multiselect = true;//设置对话框的初始目录ofd.InitialDirectory = @"C:\Users\46124\Desktop\.wav音乐";//设置对话框可筛选的文件类型的内容ofd.Filter = "文本文件|*.txt|媒体文件|*.wav|所有文件|*.*";//展示对话框ofd.ShowDialog();//获得在打开对话框中选中的路径string path = ofd.FileName;//如果路径为空,则立即跳出循环if (path == ""){return;}using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read)){byte[] buffer = new byte[1024 * 1024 * 5];//实际读取到的字节数int readbyte = fsRead.Read(buffer, 0, buffer.Length);textBox1.Text = Encoding.Default.GetString(buffer, 0, readbyte);}MessageBox.Show("打开成功");}/// <summary>/// 保存对话框/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void btnSave_Click(object sender, EventArgs e){SaveFileDialog sfd = new SaveFileDialog();sfd.Title = "请选择要保存的路径";sfd.InitialDirectory = @"C:\Users\46124\Desktop";sfd.Filter = "文本文件|*.txt|媒体文件|*.wav|所有文件|*.*";//获取保存文件的路径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("保存成功");}/// <summary>/// 字体对话框/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button1_Click_1(object sender, EventArgs e){FontDialog fd = new FontDialog();fd.ShowDialog();textBox1.Font = fd.Font;//textBox1.ForeColor = fd.Color;}/// <summary>/// 前景色颜色对话框/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button2_Click(object sender, EventArgs e){ColorDialog cd = new ColorDialog();cd.ShowDialog();textBox1.ForeColor = cd.Color;}/// <summary>/// 背景色颜色对话框/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void button3_Click(object sender, EventArgs e){ColorDialog cd = new ColorDialog();cd.ShowDialog();textBox1.BackColor = cd.Color;}}}
