当前位置: 移动技术网 > IT编程>开发语言>c# > c# 共享状态的文件读写实现代码

c# 共享状态的文件读写实现代码

2019年07月18日  | 移动技术网IT编程  | 我要评论
复制代码 代码如下:

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();
}
}
}
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网