以流方式读写文件:文件菜单打开一个文件,文件内容显示在RichTexBox中,执行复制、剪切、粘贴后,通过文件菜单可以保存修改后的文件。

时间:2023-03-08 21:24:37
以流方式读写文件:文件菜单打开一个文件,文件内容显示在RichTexBox中,执行复制、剪切、粘贴后,通过文件菜单可以保存修改后的文件。

以流方式读写文件:文件菜单打开一个文件,文件内容显示在RichTexBox中,执行复制、剪切、粘贴后,通过文件菜单可以保存修改后的文件。

MainWindow.xaml文件

 <Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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"
Title="演示以流方式读写文件" Height="" Width="">
<Grid> <DockPanel>
<Menu DockPanel.Dock="Top" FontSize="">
<MenuItem Header="_文件" FontSize="">
<MenuItem Header="_打开" Click="OnOpenFile"/>
<MenuItem Header="_保存" Click="OnSaveFile"/>
<Separator/>
<MenuItem Header="退出" Click="OnExit"/>
</MenuItem>
<MenuItem Header="_编辑" FontSize=""> <MenuItem Header="撤销" Command="Undo"/>
<Separator/>
<MenuItem Header="剪切" Command="Cut"/>
<MenuItem Header="复制" Command="Copy"/>
<MenuItem Header="粘贴" Command="Paste"/>
</MenuItem>
</Menu>
<RichTextBox VerticalScrollBarVisibility="Visible" Name="richTextBox1" Margin="0,5,0,0"></RichTextBox>
</DockPanel> </Grid> </Window>

MainWindow.xaml.cs文件

 using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace csharp
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void RichTextBox_TextChanged(object sender, TextChangedEventArgs e)
{ }
public void LoadText()
{
string textFile = @"C:\Users\yinyin\Desktop\hw\a.txt";//默认打开此文件
FileStream fs;
if (File.Exists(textFile))
{
fs = new FileStream(textFile, FileMode.Open, FileAccess.Read);
using (fs)
{
TextRange text = new TextRange(richTextBox1.Document.ContentStart, richTextBox1.Document.ContentEnd);
text.Load(fs, DataFormats.Text);
} }
} private static void SaveFile(string filename, RichTextBox richTextBox)
{
if (string.IsNullOrEmpty(filename))
{
throw new ArgumentNullException();
} using (FileStream stream = File.OpenWrite(filename))
{
TextRange documentTextRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd);
string dataFormat = DataFormats.Text; string ext = System.IO.Path.GetExtension(filename); if (String.Compare(ext, ".xaml", true) == )
{
dataFormat = DataFormats.Xaml;
}
else if (String.Compare(ext, ".rtf", true) == )
{
dataFormat = DataFormats.Text;
}
documentTextRange.Save(stream, dataFormat);
}
} private void OnOpenFile(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf";
ofd.Multiselect = false;
if (ofd.ShowDialog() == true)
{
LoadText();
}
} private void OnSaveFile(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "Text Files (*.txt; *.xaml; *.rtf)|*.txt;*.xaml;*.rtf";
if (sfd.ShowDialog() == true)
{
SaveFile(sfd.SafeFileName, richTextBox1);
}
} private void OnExit(object sender, EventArgs e)
{
this.Close();
} }
}

运行步骤

1.              在菜单栏中找到“打开” MenuItem;

2.              找到“a.txt”文件并打开;

3.              编辑(或通过编辑菜单编辑)内容;

4.              通过文件菜单栏编辑后的文件;

5.              将编辑后的文件保存;

6.              查看保存后的文件;

7.              查看修改后的文件,可以看到保存的文件的内容是被编辑后的。