打开对话框:
    image.png

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. using System.IO;
    11. namespace _117_Dialog打开对话框
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. }
    19. private void button1_Click(object sender, EventArgs e)
    20. {
    21. //点击弹出对话框
    22. OpenFileDialog ofd = new OpenFileDialog();
    23. //设置对话框标题
    24. ofd.Title = "选择你要打开的文件O(∩_∩)O";
    25. //设置对话框可以多选
    26. ofd.Multiselect = true;
    27. //设置对话框的初始目录
    28. ofd.InitialDirectory = @"C:\Users\46124\Desktop\.wav音乐";
    29. //设置对话框可筛选的文件类型的内容
    30. ofd.Filter = "媒体文件|*.wav|文本文件|*.txt|所有文件|*.*";
    31. //展示对话框
    32. ofd.ShowDialog();
    33. //获得在打开对话框中选中的路径
    34. string path = ofd.FileName;
    35. //如果路径为空,则立即跳出循环
    36. if (path=="")
    37. {
    38. return;
    39. }
    40. using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
    41. {
    42. byte[] buffer = new byte[1024 * 1024 * 5];
    43. //实际读取到的字节数
    44. int readbyte = fsRead.Read(buffer, 0, buffer.Length);
    45. textBox1.Text = Encoding.UTF8.GetString(buffer, 0, readbyte);
    46. }
    47. }
    48. }
    49. }

    打开和保存文件(目前只实现文本文件)
    image.png

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. using System.IO;
    11. namespace _117_Dialog打开对话框
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. }
    19. private void button1_Click(object sender, EventArgs e)
    20. {
    21. //点击弹出对话框
    22. OpenFileDialog ofd = new OpenFileDialog();
    23. //设置对话框标题
    24. ofd.Title = "选择你要打开的文件O(∩_∩)O";
    25. //设置对话框可以多选
    26. ofd.Multiselect = true;
    27. //设置对话框的初始目录
    28. ofd.InitialDirectory = @"C:\Users\46124\Desktop\.wav音乐";
    29. //设置对话框可筛选的文件类型的内容
    30. ofd.Filter = "文本文件|*.txt|媒体文件|*.wav|所有文件|*.*";
    31. //展示对话框
    32. ofd.ShowDialog();
    33. //获得在打开对话框中选中的路径
    34. string path = ofd.FileName;
    35. //如果路径为空,则立即跳出循环
    36. if (path == "")
    37. {
    38. return;
    39. }
    40. using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
    41. {
    42. byte[] buffer = new byte[1024 * 1024 * 5];
    43. //实际读取到的字节数
    44. int readbyte = fsRead.Read(buffer, 0, buffer.Length);
    45. textBox1.Text = Encoding.Default.GetString(buffer, 0, readbyte);
    46. }
    47. MessageBox.Show("打开成功");
    48. }
    49. private void btnSave_Click(object sender, EventArgs e)
    50. {
    51. SaveFileDialog sfd = new SaveFileDialog();
    52. sfd.Title = "请选择要保存的路径";
    53. sfd.InitialDirectory = @"C:\Users\46124\Desktop";
    54. sfd.Filter = "文本文件|*.txt|媒体文件|*.wav|所有文件|*.*";
    55. //获取保存文件的路径
    56. sfd.ShowDialog();
    57. string path = sfd.FileName;
    58. if (path=="")
    59. {
    60. return;
    61. }
    62. using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
    63. {
    64. byte[] buffer=Encoding.Default.GetBytes(textBox1.Text);
    65. fsWrite.Write(buffer, 0, buffer.Length);
    66. }
    67. MessageBox.Show("保存成功");
    68. }
    69. }
    70. }

    添加了前景背景色,字体
    image.png

    1. using System;
    2. using System.Collections.Generic;
    3. using System.ComponentModel;
    4. using System.Data;
    5. using System.Drawing;
    6. using System.Linq;
    7. using System.Text;
    8. using System.Threading.Tasks;
    9. using System.Windows.Forms;
    10. using System.IO;
    11. namespace _117_Dialog打开对话框
    12. {
    13. public partial class Form1 : Form
    14. {
    15. public Form1()
    16. {
    17. InitializeComponent();
    18. }
    19. /// <summary>
    20. /// 打开对话框
    21. /// </summary>
    22. /// <param name="sender"></param>
    23. /// <param name="e"></param>
    24. private void button1_Click(object sender, EventArgs e)
    25. {
    26. //点击弹出对话框
    27. OpenFileDialog ofd = new OpenFileDialog();
    28. //设置对话框标题
    29. ofd.Title = "选择你要打开的文件O(∩_∩)O";
    30. //设置对话框可以多选
    31. ofd.Multiselect = true;
    32. //设置对话框的初始目录
    33. ofd.InitialDirectory = @"C:\Users\46124\Desktop\.wav音乐";
    34. //设置对话框可筛选的文件类型的内容
    35. ofd.Filter = "文本文件|*.txt|媒体文件|*.wav|所有文件|*.*";
    36. //展示对话框
    37. ofd.ShowDialog();
    38. //获得在打开对话框中选中的路径
    39. string path = ofd.FileName;
    40. //如果路径为空,则立即跳出循环
    41. if (path == "")
    42. {
    43. return;
    44. }
    45. using (FileStream fsRead = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
    46. {
    47. byte[] buffer = new byte[1024 * 1024 * 5];
    48. //实际读取到的字节数
    49. int readbyte = fsRead.Read(buffer, 0, buffer.Length);
    50. textBox1.Text = Encoding.Default.GetString(buffer, 0, readbyte);
    51. }
    52. MessageBox.Show("打开成功");
    53. }
    54. /// <summary>
    55. /// 保存对话框
    56. /// </summary>
    57. /// <param name="sender"></param>
    58. /// <param name="e"></param>
    59. private void btnSave_Click(object sender, EventArgs e)
    60. {
    61. SaveFileDialog sfd = new SaveFileDialog();
    62. sfd.Title = "请选择要保存的路径";
    63. sfd.InitialDirectory = @"C:\Users\46124\Desktop";
    64. sfd.Filter = "文本文件|*.txt|媒体文件|*.wav|所有文件|*.*";
    65. //获取保存文件的路径
    66. sfd.ShowDialog();
    67. string path = sfd.FileName;
    68. if (path=="")
    69. {
    70. return;
    71. }
    72. using (FileStream fsWrite = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
    73. {
    74. byte[] buffer=Encoding.Default.GetBytes(textBox1.Text);
    75. fsWrite.Write(buffer, 0, buffer.Length);
    76. }
    77. MessageBox.Show("保存成功");
    78. }
    79. /// <summary>
    80. /// 字体对话框
    81. /// </summary>
    82. /// <param name="sender"></param>
    83. /// <param name="e"></param>
    84. private void button1_Click_1(object sender, EventArgs e)
    85. {
    86. FontDialog fd = new FontDialog();
    87. fd.ShowDialog();
    88. textBox1.Font = fd.Font;
    89. //textBox1.ForeColor = fd.Color;
    90. }
    91. /// <summary>
    92. /// 前景色颜色对话框
    93. /// </summary>
    94. /// <param name="sender"></param>
    95. /// <param name="e"></param>
    96. private void button2_Click(object sender, EventArgs e)
    97. {
    98. ColorDialog cd = new ColorDialog();
    99. cd.ShowDialog();
    100. textBox1.ForeColor = cd.Color;
    101. }
    102. /// <summary>
    103. /// 背景色颜色对话框
    104. /// </summary>
    105. /// <param name="sender"></param>
    106. /// <param name="e"></param>
    107. private void button3_Click(object sender, EventArgs e)
    108. {
    109. ColorDialog cd = new ColorDialog();
    110. cd.ShowDialog();
    111. textBox1.BackColor = cd.Color;
    112. }
    113. }
    114. }