以流方式读写文件:文件菜单打开一个文件,文件内容显示在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. 查看修改后的文件,可以看到保存的文件的内容是被编辑后的。
以流方式读写文件:文件菜单打开一个文件,文件内容显示在RichTexBox中,执行复制、剪切、粘贴后,通过文件菜单可以保存修改后的文件。的更多相关文章
- 背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri
[源码下载] 背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri 作者:webabcd 介绍背水一战 Windows 10 之 关联启动 ...
- Qt之密码框不可选中、复制、粘贴、无右键菜单等
简述 在做用户登录.修改密码的时候,往往会用到密码框,其中一些功能要求与普通的输入框不同,例如:不能选中.复制.粘贴.无右键菜单等功能,当然设置密码不可见是必须的! 下面介绍两种方式来实现相同的效果. ...
- 【Qt】Qt之密码框不可选中、复制、粘贴、无右键菜单等【转】
简述 在做用户登录.修改密码的时候,往往会用到密码框,其中一些功能要求与普通的输入框不同,例如:不能选中.复制.粘贴.无右键菜单等功能,当然设置密码不可见是必须的! 下面介绍两种方式来实现相同的效果. ...
- Qt 密码框不可选中、复制、粘贴、无右键菜单等
在做用户登录.修改密码的时候,往往会用到密码框,其中一些功能要求与普通的输入框不同. 例如:不能选中.复制.粘贴.无右键菜单等功能,当然设置密码不可见是必须的! 一般的密码框:(默认 可以选中,复制, ...
- java servlet上传文件并把文件内容显示在网页中
servlet3.0(JDK1.6)自带的API即可实现本地文件的上传,Servlet3.0新增了Part接口,HttpServletRequest的getPart()方法取得Part实现对象.下面我 ...
- 表单禁用复制、粘贴、及右击菜单(contextmenu、oncopy、oncut、onpaste、onselectstart)
禁用右键菜单,可以使用oncontextmenu属性: <textarea oncontextmenu="return false"></textarea> ...
- VBA 按照文件类型名称打开一个文件
Application.GetOpenFilename(fileFilter, fileIndex, fileSelectTitle, button, False) fileFilter: 指定能够被 ...
- XML文件解析并利用SimpleAdapter将解析结果显示在Activity中
首先创建一个实体类 Mp3Info用来存储解析的XML文件中的内容: public class Mp3Info implements Serializable{ private static fina ...
- modalDialog的使用,图片切换,点击图片时打开一个窗体,并显示信息
//主窗体 //与open的区别:1.参数二是传递的参数 2.属性设置格式:属性=属性值; 3.dialogHeight与dialogWidth没有单位,即需要自己加上px //window.show ...
随机推荐
- 强大又简单的响应式框架——Foundation 网格系统
前端框架——Foundation 简介 Foundation 用于开发响应式的 HTML, CSS and JavaScript 框架. Foundation 是一个易用.强大而且 ...
- SSM 五:Spring核心概念
第五章:Spring核心概念 一.Spring Ioc 优点: 1.低侵入式设计 2.独立于各种应用服务器 3.依赖注入特性将组建关系透明化,降低耦合度 4.面向切面编程的特性允许将通用性任务集中式处 ...
- python进阶------进程线程(五)
Python中的IO模型 同步(synchronous) IO和异步(asynchronous) IO,阻塞(blocking) IO和非阻塞(non-blocking)IO分别是什么,到底有什么区别 ...
- border-radio属性
boreder-radio属性是css3的新增属性,可以设置圆角的边框. <head> <style type="text/css"> *{ margin ...
- Oracle11g静默安装
静默安装Oracle11G 前期准备 1虚拟机准备: Centos 6.5 64位 内存2G 硬盘30G Cpu 4核 Hostname silent 将安装包放到/tmp/oracle目录下 2.软 ...
- 开始你的第一个npm脚本工具
在实际开发中,一般刚开始一个项目或者刚接手一个项目,我们会运行 npm install 下载安装所有依赖, 在实际开发中,可能也会使用各种命令行-- 来提高我们开发的效率. 与它相处了这么久,你真的了 ...
- js 获取每月有几周,根据年月周获取该周从周一到周日的日期等方法
本文基于react-native 本人在用react-native写一个关于课程表的APP时需要课程表按照日期周期显示,网上查了许多方法,都没有达到自己想要的效果,根据一些方法的参考,再根据自己思维写 ...
- Python入门 - 生成随机数
生成随机数是编程中经常用到的功能,下面讲几种常用的随机函数randint,uniform, randrange: 一.生成随机整数 randint import random a = random. ...
- QQ空间掉帧率优化实战
商业转载请联系腾讯WeTest获得授权,非商业转载请注明出处. WeTest 导读 空间新业务需求日益增多,在业务开发阶段的疏忽,或者是受到其他业务的影响(比如一些非空间的业务网络回包或者逻辑在主线程 ...
- PHP扩展安装方法
php扩展安装方法极简单. 也遵循3大步.但多出一个phpize的步骤. 1.pecl.php.net 在右上解的输入框 中输入需要的扩展 比如 redis 2.搜索完成后会看到两个蓝色的框 ...