最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
C#中读取共享状态的文件读写操作程序
时间:2022-06-25 08:22:05 编辑:袖梨 来源:一聚教程网
| 代码如下 | 复制代码 |
using System.IO; using System.Text; namespace LucienBao.Commons { public static class FileHelper { public static string ShareRead(string file, Encoding encoding) { string content = string.Empty; FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); try { if (fs.CanRead) { byte[] buffer = new byte[fs.Length]; fs.Read(buffer, 0, buffer.Length); content = encoding.GetString(buffer); } } finally { fs.Close(); fs.Dispose(); } return content; } public static void ShareAppend(string content, string file, Encoding encoding) { ShareWrite(content, file, encoding, FileMode.Append); } public static void ShareWrite(string content, string file, Encoding encoding, FileMode fileMode) { FileStream fs = new FileStream(file, fileMode, FileAccess.Write, FileShare.Read); try { if (fs.CanWrite) { byte[] buffer = encoding.GetBytes(content); if (buffer.Length > 0) { fs.Write(buffer, 0, buffer.Length); fs.Flush(); } } } finally { fs.Close(); fs.Dispose(); } } } } |
|
相关文章
- 快手官网网页版入口-快手网页版在线登录直达 01-05
- 羊蹄山之魂新手开荒盔甲推荐 羊蹄山之魂新手盔甲怎么选 01-05
- 囧次元官网入口在哪-最新官网入口分享 01-05
- 羊蹄山之魂全风铃位置一览 羊蹄山之魂风铃位置详情 01-05
- 126邮箱网页版登录入口-网易126邮箱手机端登录 01-05
- 羊蹄山之魂地下祭坛在哪里 羊蹄山之魂地下祭坛位置详情 01-05