C# 实用文件存储
来源:互联网 发布:槽钢抗弯强度计算软件 编辑:程序博客网 时间:2024/06/14 06:14
这里的实例是C# 编写记事本
第一:解决文件读写问题
1.读:文件-打开 click时间是新建一个OpenFileDialog提示框选取文件,读取的方式是采用了FileStream文件流的方式
click方法
<span style="white-space:pre"></span>OpenFileDialog dlg = new OpenFileDialog(); if (dlg.ShowDialog() == DialogResult.OK)//判断是否点击了确定按钮 textBox1.Text=tools.FileRW.ReadText(dlg.FileName,EncodingUse); fName = dlg.SafeFileName;//全局文件名 FilePath = dlg.FileName.Substring(0, dlg.FileName.IndexOf(fName));//全局文件路径FileStream读方法
/// <summary> /// //c#文件流读文件 /// </summary> /// <param name="FilePath"></param> /// <param name="ReadEncoding">{"Default","UTF-8","ANSI","Unicode","Unicode big encode"}--0,1 ,2 ,3 ,4</param> /// <returns></returns> public static string ReadText(string FilePath,int ReadEncoding) { using (FileStream fsRead = new FileStream(@FilePath, FileMode.Open)) { int fsLen = (int)fsRead.Length; byte[] heByte = new byte[fsLen]; int r = fsRead.Read(heByte, 0, heByte.Length); string myStr=""; myStr = System.Text.Encoding.Default.GetString(heByte); return myStr; } }2.存:直接写入文件即可
<span style="white-space:pre"></span>/// <summary> /// C#文件流写文件,默认追加FileMode.Append ,但是这里没有追加Append /// </summary> /// <param name="text">要写入的字符串</param> /// <param name="FilePath">文件路径+文件名</param> public static void WriteText(string text,string FilePath) { byte[] myByte = System.Text.Encoding.Default.GetBytes(text); using (FileStream fsWrite = new FileStream(@FilePath, FileMode.Create, FileAccess.Write)) { fsWrite.Write(myByte, 0, myByte.Length); }; }
3,另存:采用了saveFileDialog和Filestream进行操作
saveFileDialog方法
<span style="white-space:pre"></span> SaveFileDialog sfd = new SaveFileDialog(); sfd.Title = "另存为What?";// sfd.InitialDirectory =FilePath;//设置打开路径 sfd.Filter = "txt files(*.txt)|*.txt|xls files(*.xls)|*.xls|All files(*.*)|*.*";//设置可选文件保存类型 sfd.FileName = fName;//set value file name sfd.DefaultExt = "txt";//set value file suffix sfd.AddExtension = true;//<span style="font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; font-size: 13px; line-height: 19.5px;">获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名</span> sfd.RestoreDirectory = true;//set y or n remenber last file open path if (sfd.ShowDialog() == DialogResult.OK) { string localFilePath = sfd.FileName.ToString();//get the dialog save file path string savename= localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);//get file save name tools.FileRW.WriteText(textBox1.Text, FilePath, savename);//write out }文件另存方法
<span style="white-space:pre"></span>/// <summary> /// File other write /// </summary> /// <param name="text">要写入的文件内容</param> /// <param name="FilePath">文件路径</param> /// <param name="FileName">文件名</param> public static void WriteText(string text, string FilePath, string FileName) { byte[] myByte = System.Text.Encoding.Default.GetBytes(text); string bufferName=FilePath+FileName; using (FileStream fsWrite = new FileStream(@bufferName, FileMode.Create, FileAccess.Write)) { fsWrite.Write(myByte, 0, myByte.Length); }; }
0 0
- C# 实用文件存储
- 实用-实现文件存储的JS函数
- C#文件存储要素汇总
- C#实现文件二进制存储
- C#打开文件或存储文件
- C#序列化读取和存储文件
- 将mater库中的系统存储过程批量生成*.sql文件--通用且非常实用
- C# 实用代码01
- C#实用小技巧
- 实用的C#函数
- C# dataTable实用例
- C#数据库实用
- C#实用方法集合
- C#实用函数大全
- C# dataTable实用例
- C#实用函数大全
- C#实用函数大全
- 实用的存储过程之一
- 绘图板 手写板
- CDISC SDTM SU domain 学习笔记
- Python学习笔记:字符串(str)基本内容
- Python2.7 + beautifulSoup 4.4.1 安装配置
- bom表
- C# 实用文件存储
- sdutoj 3302 效率至上
- Android Studio gradle更新地址
- Spring HTTP Invoker使用介绍
- 小巴孩 母亲节祝福
- Timer定时器
- 专访|腾讯、阿里、adidas的服务方UID WORKS
- 拍照权限被第三方禁用
- UNITY Rigidbody 参数设置