以流方式读写文件:文件菜单打开一个文件,文件内容显示在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. 机器学习数学|偏度与峰度及其python实现

    机器学习中的数学 觉得有用的话,欢迎一起讨论相互学习~Follow Me 原创文章,如需转载请保留出处 本博客为七月在线邹博老师机器学习数学课程学习笔记 矩 对于随机变量X,X的K阶原点矩为 \[E( ...

  2. struts2+spring3+hibernate3+mysql简单登录实现

    1.导入相关的jar包 2.建立数据库 1 create table account( 2 id int(10), 3 user varchar(50), 4 paw varchar(50) 5 ); ...

  3. Nodejs mongodb 管理组件adminmongodb

    强大的 nodejs的mongodb管理工具,强大到即下即用: 安装需求: 1.git命令获取组件包,git clone https://github.com/mrvautin/adminMongo. ...

  4. [转载] Java集合框架之小结

    转载自http://jiangzhengjun.iteye.com/blog/553191 1.Java容器类库的简化图,下面是集合类库更加完备的图.包括抽象类和遗留构件(不包括Queue的实现): ...

  5. Python之matplotlib学习(二)

    例子6.中文标签测试 #!/usr/bin/env python2.7 #-*- coding:utf-8 -*- import matplotlib.pyplot as plt import num ...

  6. 2017/10/10 jar包错误

    Description    Resource    Path    Location    Type Archive for required library: 'WebContent/WEB-IN ...

  7. 这次真的忽略了一些ActiveMQ内心的娇艳

    好久没总结了,内心有点空虚了,所以今天主要给园里的朋友们分享一点儿这几天使用ActiveMQ过程中踩过的小坑,虽然说这东西简单易用,代码几行配置也就几行,问题不大但是后果有点严重,所以就要必要总结一下 ...

  8. ECMAScript6词法

    ES6简介 ECMAScript 6.0(简称 ES6)是 JavaScript 语言的下一代标准,它于2015 年 6 月正式发布.ECMAScript 和 JavaScript 的关系是,前者是后 ...

  9. python基础教程——dict和set

    dict python内置字典:dict,全称dictionary,在其他语言中称为map,使用键值对存储. ex: d = {'xiaoli' : 95 , 'xiaoming' : 98 , 'x ...

  10. Next week plan

    1.get a job 2.write a high performance chatroom with encryption.  Use scala. Next Week turn to Rust