文件读取保存类
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CommHelper
{
public class FilesAccess
{
public FilesAccess()
{ }
#region
/// <summary>
/// 把文件内容写入到物理位置
/// </summary>
/// <param name="as_filename"></param>
/// <param name="a_FileData"></param>
/// <returns></returns>
public static string WriteFile(string as_filename, byte[] a_FileData)
{
string sRet = "";
if (as_filename == "")
{
sRet = "请指定路径和文件名";
return sRet;
}
try
{
System.IO.FileStream objFile = new System.IO.FileStream(as_filename,
System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite, System.IO.FileShare.ReadWrite);
System.IO.BinaryWriter objSW = new BinaryWriter(objFile, System.Text.Encoding.Unicode);
objSW.BaseStream.Write(a_FileData, 0, a_FileData.Length);
objSW.Close();
objSW = null;
objFile.Close();
objFile = null;
}
catch (Exception ex)
{
sRet = ex.Message;
}
return sRet;
}
/// <summary>
/// 把文件内容写入到物理位置
/// </summary>
/// <param name="as_filename"></param>
/// <param name="as_FileData"></param>
/// <returns></returns>
public static string WriteFile(string as_filename,string as_FileData)
{
string sRet="";
if (as_filename == "")
{
sRet = "请指定路径和文件名";
return sRet;
}
try
{
System.IO.FileStream objFile=new System.IO.FileStream(as_filename,
System.IO.FileMode.OpenOrCreate ,System.IO.FileAccess.ReadWrite ,System.IO.FileShare.ReadWrite );
System.IO.BinaryWriter objSW=new BinaryWriter(objFile,System.Text.Encoding.Unicode);
byte[] tmpData=System.Text.Encoding.Unicode.GetBytes(as_FileData);
objSW.BaseStream.Write(tmpData,0,tmpData.Length);
objSW.Close();
objSW=null;
objFile.Close();
objFile=null;
sRet="";
}
catch(Exception exp)
{
sRet=exp.Message;
}
return sRet;
}
/// <summary>
/// 读取物理位置文件内容
/// </summary>
/// <param name="as_filename"></param>
/// <returns></returns>
public static byte[] ReadFile(string as_filename)
{
byte[] tmpData = null;
if (as_filename == "")
{
return tmpData;
}
byte[] tmpAddData = new byte[1024 * 1024];
int intNum = 0;
int intReadLen = 0;
long lngLen = 0;
System.IO.FileStream objFile = new System.IO.FileStream(as_filename,
System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
System.IO.BinaryReader objSR = new System.IO.BinaryReader(objFile, System.Text.Encoding.Unicode);
tmpData = new byte[objSR.BaseStream.Length];
while (true)
{
intReadLen = objSR.Read(tmpAddData, 0, tmpAddData.Length);
Array.Copy(tmpAddData, 0, tmpData, lngLen, intReadLen);
lngLen = lngLen + intReadLen;
intNum = intNum + 1;
if (intReadLen <= 0)
{
break;
}
}
objSR.Close();
objSR = null;
objFile.Close();
objFile = null;
return tmpData;
}
/// <summary>
/// 读取物理位置文件的内容
/// </summary>
/// <param name="as_filename"></param>
/// <param name="as_FileData"></param>
/// <returns></returns>
public static string ReadFile(string as_filename, out byte[] as_FileData)
{
string sRet = "";
as_FileData = null;
byte[] tmpData = null;
if (as_filename == "")
{
sRet = "请指定路径和文件名";
return sRet;
}
try
{
byte[] tmpAddData = new byte[1024 * 1024];
int intNum = 0;
int intReadLen = 0;
long lngLen = 0;
System.IO.FileStream objFile = new System.IO.FileStream(as_filename,
System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
System.IO.BinaryReader objSR = new System.IO.BinaryReader(objFile, System.Text.Encoding.Unicode);
tmpData = new byte[objSR.BaseStream.Length];
while (true)
{
intReadLen = objSR.Read(tmpAddData, 0, tmpAddData.Length);
Array.Copy(tmpAddData, 0, tmpData, lngLen, intReadLen);
lngLen = lngLen + intReadLen;
intNum = intNum + 1;
if (intReadLen <= 0)
{
break;
}
}
objSR.Close();
objSR = null;
objFile.Close();
objFile = null;
as_FileData = tmpData;
}
catch (Exception ex)
{
sRet = ex.Message.ToString();
as_FileData = null;
}
return sRet;
}
/// <summary>
/// 从物理位置读取文件内容
/// </summary>
/// <param name="as_filename"></param>
/// <param name="asFileData"></param>
/// <returns></returns>
public static string ReadFile(string as_filename, out string as_FileData)
{
string sRet = "";
as_FileData = "";
try
{
if (as_filename == "")
{
sRet = "请指定路径和文件名";
return sRet;
}
byte[] tmpData;
byte[] tmpAddData = new byte[1024 * 1024];
int intNum = 0;
int intReadLen = 0;
long lngLen = 0;
System.IO.FileStream objFile = new System.IO.FileStream(as_filename,
System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
System.IO.BinaryReader objSR = new System.IO.BinaryReader(objFile, System.Text.Encoding.Unicode);
tmpData = new byte[objSR.BaseStream.Length];
while (true)
{
intReadLen = objSR.Read(tmpAddData, 0, tmpAddData.Length);
Array.Copy(tmpAddData, 0, tmpData, lngLen, intReadLen);
lngLen = lngLen + intReadLen;
intNum = intNum + 1;
if (intReadLen <= 0)
{
break;
}
}
as_FileData = System.Text.Encoding.Unicode.GetString(tmpData);
objSR.Close();
objSR = null;
objFile.Close();
objFile = null;
sRet = "";
}
catch (Exception eRead)
{
sRet = eRead.Message.ToString();
as_FileData = "";
}
return sRet;
}
/// <summary>
/// 把文件内容写入到物理位置
/// </summary>
/// <param name="as_filename"></param>
/// <param name="as_FileData"></param>
/// <returns></returns>
public static string WriteFile_UTF8(string as_filename,string as_FileData)
{
string sRet="";
try
{
if(as_filename=="")
{
sRet="请指定路径和文件名";
return sRet;
}
System.IO.FileStream objFile=new System.IO.FileStream(as_filename,
System.IO.FileMode.OpenOrCreate ,System.IO.FileAccess.ReadWrite ,System.IO.FileShare.ReadWrite );
System.IO.BinaryWriter objSW=new BinaryWriter(objFile,System.Text.Encoding.UTF8);
byte[] tmpData=System.Text.Encoding.UTF8.GetBytes(as_FileData);
objSW.BaseStream.Write(tmpData,0,tmpData.Length);
objSW.Close();
objSW=null;
objFile.Close();
objFile=null;
sRet="";
}
catch(Exception exp)
{
sRet=exp.Message;
}
return sRet;
}
#endregion
}
}