usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
///<summary>
///FileOperate的摘要说明
///</summary>
publicclassFileOperate
{

publicFileOperate()
{
//
//TODO:在此处添加构造函数逻辑
//
}
//生成一个文件
publicstaticboolWriteFile(stringFileContent,stringFilePath)
{
try
{
stringstrPath=System.Web.HttpContext.Current.Server.MapPath(FilePath);
if(System.IO.File.Exists(strPath))
{
System.IO.File.Delete(strPath);
}
System.IO.FileStreamFs=System.IO.File.Create(strPath);
byte[]bcontent=System.Text.Encoding.GetEncoding("GB2312").GetBytes(FileContent);
Fs.Write(bcontent,0,bcontent.Length);
Fs.Close();
Fs=null;
returntrue;
}
catch
{
returnfalse;
}
finally
{
}
}
//读入文件
publicstaticstringReadWrite(stringFilePath)
{
try
{
stringstrPath=System.Web.HttpContext.Current.Server.MapPath(FilePath);
if(System.IO.File.Exists(strPath))
{
stringstrRead=System.IO.File.ReadAllText(strPath,System.Text.Encoding.GetEncoding("gb2312"));
returnstrRead;
}
else
{
return"false";
}
}
catch
{
return"false";
}
finally
{
}
}
//向文件中增加内容
publicstaticboolUpdateFile(stringFilePath,stringFileContent)
{
try
{
stringstrPath=System.Web.HttpContext.Current.Server.MapPath(FilePath).ToString();
if(!System.IO.File.Exists(strPath))
{
//文件不存在,生成文件
System.IO.FileStreamFs=System.IO.File.Create(strPath);
byte[]bcontent=System.Text.Encoding.GetEncoding("GB2312").GetBytes(FileContent);
Fs.Write(bcontent,0,bcontent.Length);
Fs.Close();
Fs=null;
returntrue;
}
System.IO.FileStreamFileRead=newSystem.IO.FileStream(strPath,System.IO.FileMode.Open,System.IO.FileAccess.ReadWrite);
System.IO.StreamReaderFileReadWord=newSystem.IO.StreamReader(FileRead,System.Text.Encoding.Default);
stringOldString=FileReadWord.ReadToEnd().ToString();
OldString=OldString+FileContent;
//把新的内容重新写入
System.IO.StreamWriterFileWrite=newSystem.IO.StreamWriter(FileRead,System.Text.Encoding.Default);
FileWrite.Write(OldString);
//关闭
FileWrite.Close();
FileReadWord.Close();
FileRead.Close();
returntrue;
}
catch
{
//throw;
returnfalse;
}
}
//删除文件
publicstaticboolDeleteFile(stringFilePath)
{
try
{
stringstrPath=System.Web.HttpContext.Current.Server.MapPath(FilePath).ToString();
if(System.IO.File.Exists(strPath))
{
System.IO.File.Delete(strPath);
}
returntrue;
}
catch
{
returnfalse;
}
finally
{
}
}
//创建文件夹
publicstaticboolCreateFolder(stringFolderName)
{
if(FolderName.Trim().Length>0)
{
try
{
stringFolderPath=System.Web.HttpContext.Current.Server.MapPath(FolderName);
if(!System.IO.Directory.Exists(FolderPath))
{
System.IO.Directory.CreateDirectory(FolderPath);
returntrue;
}
else
{
returntrue;
}
}
catch
{
returnfalse;
}
finally
{
}
}
else
{
returnfalse;
}
}
//删除整个文件夹及其字文件夹和文件
publicstaticboolDeleteFolder(stringFolderName)
{
if(FolderName.Trim().Length>0)
{
try
{
stringFolderPath=System.Web.HttpContext.Current.Server.MapPath(FolderName);
if(System.IO.Directory.Exists(FolderPath))
{
System.IO.Directory.Delete(FolderPath,true);
returntrue;
}
else
{
returntrue;
}
}
catch
{
returnfalse;
}
finally
{
}
}
else
{
returnfalse;
}
}
//删除整个文件夹及其字文件夹和文件(验证没成功)
publicstaticboolDeleParentFolder(stringFolderPathName)
{
try
{
stringstrPath=System.Web.HttpContext.Current.Server.MapPath(FolderPathName).ToString();
System.IO.DirectoryInfoDelFolder=newSystem.IO.DirectoryInfo(strPath);
if(DelFolder.Exists)
{
DelFolder.Delete();
}
returntrue;
}
catch
{
returnfalse;
}
}
//遍历一个目录下的全部目录
publicstaticDataTableGetDir(stringFilePath)
{
DataTabletb=newDataTable();
tb.Columns.Add("name",typeof(string));
tb.Columns.Add("fullname",typeof(string));
stringstrPath=System.Web.HttpContext.Current.Server.MapPath(FilePath).ToString();
System.IO.DirectoryInfodir=newSystem.IO.DirectoryInfo(strPath);
foreach(System.IO.DirectoryInfodChildindir.GetDirectories("*"))
{
//如果用GetDirectories("ab*"),那么全部以ab开头的目录会被显示
DataRowDr=tb.NewRow();
Dr["name"]=dChild.Name;//打印目录名
Dr["fullname"]=dChild.FullName;//打印路径和目录名
tb.Rows.Add(Dr);
}
returntb;
}
//遍历一个目录下的全部目录
publicstaticDataTableGetFile(stringFilePath)
{
DataTabletb=newDataTable();
tb.Columns.Add("name",typeof(string));
tb.Columns.Add("fullname",typeof(string));
stringstrPath=System.Web.HttpContext.Current.Server.MapPath(FilePath).ToString();
System.IO.DirectoryInfodir=newSystem.IO.DirectoryInfo(strPath);
foreach(System.IO.FileInfodChildindir.GetFiles("*"))
{
//如果用GetDirectories("ab*"),那么全部以ab开头的目录会被显示
DataRowDr=tb.NewRow();
Dr["name"]=dChild.Name;//打印目录名
Dr["fullname"]=dChild.FullName;//打印路径和目录名
tb.Rows.Add(Dr);
}
returntb;
}