以流方式读写文件:文件菜单打开一个文件,文件内容显示在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.              查看修改后的文件,可以看到保存的文件的内容是被编辑后的。

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

  1. 背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri

    [源码下载] 背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri 作者:webabcd 介绍背水一战 Windows 10 之 关联启动 ...

  2. Qt之密码框不可选中、复制、粘贴、无右键菜单等

    简述 在做用户登录.修改密码的时候,往往会用到密码框,其中一些功能要求与普通的输入框不同,例如:不能选中.复制.粘贴.无右键菜单等功能,当然设置密码不可见是必须的! 下面介绍两种方式来实现相同的效果. ...

  3. 【Qt】Qt之密码框不可选中、复制、粘贴、无右键菜单等【转】

    简述 在做用户登录.修改密码的时候,往往会用到密码框,其中一些功能要求与普通的输入框不同,例如:不能选中.复制.粘贴.无右键菜单等功能,当然设置密码不可见是必须的! 下面介绍两种方式来实现相同的效果. ...

  4. Qt 密码框不可选中、复制、粘贴、无右键菜单等

    在做用户登录.修改密码的时候,往往会用到密码框,其中一些功能要求与普通的输入框不同. 例如:不能选中.复制.粘贴.无右键菜单等功能,当然设置密码不可见是必须的! 一般的密码框:(默认 可以选中,复制, ...

  5. java servlet上传文件并把文件内容显示在网页中

    servlet3.0(JDK1.6)自带的API即可实现本地文件的上传,Servlet3.0新增了Part接口,HttpServletRequest的getPart()方法取得Part实现对象.下面我 ...

  6. 表单禁用复制、粘贴、及右击菜单(contextmenu、oncopy、oncut、onpaste、onselectstart)

    禁用右键菜单,可以使用oncontextmenu属性: <textarea oncontextmenu="return false"></textarea> ...

  7. VBA 按照文件类型名称打开一个文件

    Application.GetOpenFilename(fileFilter, fileIndex, fileSelectTitle, button, False) fileFilter: 指定能够被 ...

  8. XML文件解析并利用SimpleAdapter将解析结果显示在Activity中

    首先创建一个实体类 Mp3Info用来存储解析的XML文件中的内容: public class Mp3Info implements Serializable{ private static fina ...

  9. modalDialog的使用,图片切换,点击图片时打开一个窗体,并显示信息

    //主窗体 //与open的区别:1.参数二是传递的参数 2.属性设置格式:属性=属性值; 3.dialogHeight与dialogWidth没有单位,即需要自己加上px //window.show ...

随机推荐

  1. java三大框架项目和Redis组合使用

    已知一个已有的Struts+Spring+Hibernate项目,以前使用MySQL数据库,现在想把Redis也整合进去.1. 相关Jar文件 下载并导入以下3个Jar文件: commons-pool ...

  2. mac中利用brew实现多版本php共存以及任意切换

    1.安装brew 参考链接:https://brew.sh/index_zh-cn.html 2.安装php56 brew install homebrew/php/php56 3.配置php56 因 ...

  3. (转)Ubuntu 12.04 中安装和配置 Java JDK

    http://www.cnblogs.com/bluestorm/archive/2012/05/10/2493592.html 先去 Oracle下载Linux下的JDK压缩包,我下载的是jdk-7 ...

  4. 选择排序-Python与PHP实现版

    选择排序Python实现 import random # 生成待排序数组 a=[random.randint(1,999) for x in range(0,36)] # 选择排序 def selec ...

  5. LKD: Chapter 7 Interrupts and Interrupt Handlers

    Recently I realized my English is still far from good. So in order to improve my English, I must not ...

  6. Web登录敲门砖之sql注入

    声明:文本原创,转载请说明出处,若因本文而产生任何违法违纪行为将与本人无关.在百度.博客园.oschina.github .SegmentFault.上面都关于sql注入的文章和工具.看过很多sql注 ...

  7. CDH5.11..0安装

    1.参考: http://www.cnblogs.com/codedevelop/p/6762555.html grant all privileges on *.* to 'root'@'hostn ...

  8. 全内存的redis用习惯了?使用基于硬盘存储类似redis的nosql产品ssdb呢?

    首先说一下背景,在双十一的时候,我们系统接受X宝的订单推送,同事原先的实现方式是使用redis的List作为推送数据的承载,在非大促的场景下, 一切运行正常,内存占用大概3-4G,机器是16G内存.由 ...

  9. 深入了解Android蓝牙Bluetooth——《基础篇》

    什么是蓝牙?   也可以说是蓝牙技术.所谓蓝牙(Bluetooth)技术,实际上是一种短距离无线电技术,是由爱立信公司公司发明的.利用"蓝牙"技术,能够有效地简化掌上电脑.笔记本电 ...

  10. js的学习(window对象的使用)

    open方法: //语法:var winObj = window.open([url][,name][,options]);  //参数:url:准备在新窗口中显示那个文件.url可以为空字符串,表示 ...