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();
}
}
}
}
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
深入了解c# 匿名类型
一、什么叫做匿名类? 匿名类就是没有名字的类。匿名类不能被引用,只能再创建的时候用new语句来声明。二、匿名类的优势以及应用场景; 1、匿名类型提供了一种方...
[阅读全文]
-
-
C# 实现俄罗斯方块(附源码)
概述俄罗斯方块(tetris)是一款由俄罗斯人阿列克谢·帕基特诺夫发明的休闲游戏,帕基特诺夫爱玩拼图,从拼图游戏里得到灵感,设计出了俄罗斯方块。由于上手简单、老...
[阅读全文]
-
浅析c# 接口
接口:是指定一组函数成员而不是实现他们的引用类型。所以只能类喝啊结构来实现接口,在结成该接口的类里面必须要实现接口的所有方法接口的特点:继承于接口的类,必须要实...
[阅读全文]
-
c# 接口使用实例
用接口实现一个简单的物件的入库,出库如定义一个物流类接口,包含物件所属快递公司名称属性,物件单号属性及信息显示方法。通过物件出库类信息和物件入库类信息继承该接口...
[阅读全文]
-
-
-
-
-
网友评论