文件菜单
以流方式读写文件:文件菜单打开一个文件,文件内容显示在RichTexBox中,执行复制、剪切、粘贴后,通过文件菜单可以保存修改后的文件。
MainWindow.xaml文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <Window 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="csharp.MainWindow" 5 Title="演示以流方式读写文件" Height="312" Width="525"> 6 <Grid> 7 8 9 <DockPanel> 10 <Menu DockPanel.Dock="Top" FontSize="8"> 11 <MenuItem Header="_文件" FontSize="12"> 12 <MenuItem Header="_打开" Click="OnOpenFile"/> 13 <MenuItem Header="_保存" Click="OnSaveFile"/> 14 <Separator/> 15 <MenuItem Header="退出" Click="OnExit"/> 16 </MenuItem> 17 <MenuItem Header="_编辑" FontSize="12"> 18 19 <MenuItem Header="撤销" Command="Undo"/> 20 <Separator/> 21 <MenuItem Header="剪切" Command="Cut"/> 22 <MenuItem Header="复制" Command="Copy"/> 23 <MenuItem Header="粘贴" Command="Paste"/> 24 </MenuItem> 25 </Menu> 26 <RichTextBox VerticalScrollBarVisibility="Visible" Name="richTextBox1" Margin="0,5,0,0"></RichTextBox> 27 </DockPanel> 28 29 30 31 </Grid> 32 33 </Window>
MainWindow.xaml.cs文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using Microsoft.Win32; 2 using System; 3 using System.Collections.Generic; 4 using System.IO; 5 using System.Linq; 6 using System.Text; 7 using System.Windows; 8 using System.Windows.Controls; 9 using System.Windows.Data; 10 using System.Windows.Documents; 11 using System.Windows.Input; 12 using System.Windows.Media; 13 using System.Windows.Media.Imaging; 14 using System.Windows.Navigation; 15 using System.Windows.Shapes; 16 17 namespace csharp 18 { 19 /// <summary> 20 /// MainWindow.xaml 的交互逻辑 21 /// </summary> 22 public partial class MainWindow : Window 23 { 24 public MainWindow() 25 { 26 InitializeComponent(); 27 } 28 private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e) 29 { 30 31 } 32 public void LoadText() 33 { 34 string textFile = @"C:\Users\yinyin\Desktop\hw\a.txt";//默认打开此文件 35 FileStream fs; 36 if (File.Exists(textFile)) 37 { 38 fs = new FileStream(textFile, FileMode.Open, FileAccess.Read); 39 using (fs) 40 { 41 TextRange text = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd); 42 text.Load(fs, DataFormats.Text); 43 } 44 45 } 46 } 47 48 private static void SaveFile(string filename, RichTextBox richTextBox) 49 { 50 if (string.IsNullOrEmpty(filename)) 51 { 52 throw new ArgumentNullException(); 53 } 54 55 using (FileStream stream = File.OpenWrite(filename)) 56 { 57 TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); 58 string dataFormat = DataFormats.Text; 59 60 string ext = System.IO.Path.GetExtension(filename); 61 62 if (String.Compare(ext, ".xaml", true) == 0) 63 { 64 dataFormat = DataFormats.Xaml; 65 } 66 else if (String.Compare(ext, ".rtf", true) == 0) 67 { 68 dataFormat = DataFormats.Text; 69 } 70 documentTextRange.Save(stream, dataFormat); 71 } 72 } 73 74 75 private void OnOpenFile(object sender, EventArgs e) 76 { 77 OpenFileDialog ofd = new OpenFileDialog(); 78 ofd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf"; 79 ofd.Multiselect = false; 80 if (ofd.ShowDialog() == true) 81 { 82 LoadText(); 83 } 84 } 85 86 private void OnSaveFile(object sender, EventArgs e) 87 { 88 SaveFileDialog sfd = new SaveFileDialog(); 89 sfd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf"; 90 if (sfd.ShowDialog() == true) 91 { 92 SaveFile(sfd.SafeFileName, richTextBox1); 93 } 94 } 95 96 private void OnExit(object sender, EventArgs e) 97 { 98 this.Close(); 99 } 100 101 } 102 }
运行步骤
1. 在菜单栏中找到“打开” MenuItem;
2. 找到“a.txt”文件并打开;
3. 编辑(或通过编辑菜单编辑)内容;
4. 通过文件菜单栏编辑后的文件;
5. 将编辑后的文件保存;
6. 查看保存后的文件;
7. 查看修改后的文件,可以看到保存的文件的内容是被编辑后的。
-Megan