国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c# 共享狀態(tài)的文件讀寫實(shí)現(xiàn)代碼

2020-01-24 03:41:40
字體:
供稿:網(wǎng)友
復(fù)制代碼 代碼如下:

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();
}
}
}
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 金塔县| 澄城县| 同德县| 揭东县| 海门市| 北川| 凤翔县| 当阳市| 苍山县| 惠水县| 大埔区| 老河口市| 比如县| 福贡县| 麻栗坡县| 开远市| 炎陵县| 平阳县| 邮箱| 普格县| 安图县| 奇台县| 德州市| 阳东县| 河东区| 峨山| 塔河县| 茶陵县| 海盐县| 滨海县| 枞阳县| 正蓝旗| 东台市| 岚皋县| 松滋市| 积石山| 吴江市| 建湖县| 新宁县| 旌德县| 汪清县|