using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// FileOperate 的摘要说明
/// </summary>
public class FileOperate
{
public FileOperate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//生成一个文件
public static bool WriteFile(string FileContent,string FilePath)
{
try
{
string strPath = System.Web.HttpContext.Current.Server.MapPath(FilePath);
if(System.IO.File.Exists(strPath))
{
System.IO.File.Delete(strPath);
}
System.IO.FileStream Fs = System.IO.File.Create(strPath);
byte[] bcontent = System.Text.Encoding.GetEncoding("GB2312").GetBytes(FileContent);
Fs.Write(bcontent,0,bcontent.Length);
Fs.Close();
Fs = null;
return true;
}
catch
{
return false;
}
finally
{
}
}
//读入文件
public static string ReadWrite(string FilePath)
{
try
{
string strPath = System.Web.HttpContext.Current.Server.MapPath(FilePath);
if (System.IO.File.Exists(strPath))
{
string strRead = System.IO.File.ReadAllText(strPath, System.Text.Encoding.GetEncoding("gb2312"));
return strRead;
}
else
{
return "false";
}
}
catch
{
return "false";
}
finally
{
}
}
//向文件中增加内容
public static bool UpdateFile(string FilePath, string FileContent)
{
try
{
string strPath = System.Web.HttpContext.Current.Server.MapPath(FilePath).ToString();
if (!System.IO.File.Exists(strPath))
{
//文件不存在,生成文件
System.IO.FileStream Fs = System.IO.File.Create(strPath);
byte[] bcontent = System.Text.Encoding.GetEncoding("GB2312").GetBytes(FileContent);
Fs.Write(bcontent, 0, bcontent.Length);
Fs.Close();
Fs = null;
return true;
}
System.IO.FileStream FileRead = new System.IO.FileStream(strPath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
System.IO.StreamReader FileReadWord = new System.IO.StreamReader(FileRead, System.Text.Encoding.Default);
string OldString = FileReadWord.ReadToEnd().ToString();
OldString = OldString + FileContent;
//把新的内容重新写入
System.IO.StreamWriter FileWrite = new System.IO.StreamWriter(FileRead, System.Text.Encoding.Default);
FileWrite.Write(OldString);
//关闭
FileWrite.Close();
FileReadWord.Close();
FileRead.Close();
return true;
}
catch
{
// throw;
return false;
}
}
//删除文件
public static bool DeleteFile(string FilePath)
{
try
{
string strPath = System.Web.HttpContext.Current.Server.MapPath(FilePath).ToString();
if (System.IO.File.Exists(strPath))
{
System.IO.File.Delete(strPath);
}
return true;
}
catch
{
return false;
}
finally
{
}
}
//创建文件夹
public static bool CreateFolder(string FolderName)
{
if (FolderName.Trim().Length > 0)
{
try
{
string FolderPath = System.Web.HttpContext.Current.Server.MapPath(FolderName);
if (!System.IO.Directory.Exists(FolderPath))
{
System.IO.Directory.CreateDirectory(FolderPath);
return true;
}
else
{
return true;
}
}
catch
{
return false;
}
finally
{
}
}
else
{
return false;
}
}
//删除整个文件夹及其字文件夹和文件
public static bool DeleteFolder(string FolderName)
{
if (FolderName.Trim().Length > 0)
{
try
{
string FolderPath = System.Web.HttpContext.Current.Server.MapPath(FolderName);
if (System.IO.Directory.Exists(FolderPath))
{
System.IO.Directory.Delete(FolderPath,true);
return true;
}
else
{
return true;
}
}
catch
{
return false;
}
finally
{
}
}
else
{
return false;
}
}
// 删除整个文件夹及其字文件夹和文件 (验证没成功)
public static bool DeleParentFolder(string FolderPathName)
{
try
{
string strPath = System.Web.HttpContext.Current.Server.MapPath(FolderPathName).ToString();
System.IO.DirectoryInfo DelFolder = new System.IO.DirectoryInfo(strPath);
if (DelFolder.Exists)
{
DelFolder.Delete();
}
return true;
}
catch
{
return false;
}
}
//遍历一个目录下的全部目录
public static DataTable GetDir(string FilePath)
{
DataTable tb = new DataTable();
tb.Columns.Add("name",typeof(string));
tb.Columns.Add("fullname", typeof(string));
string strPath = System.Web.HttpContext.Current.Server.MapPath(FilePath).ToString();
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(strPath);
foreach ( System.IO.DirectoryInfo dChild in dir.GetDirectories("*"))
{
//如果用GetDirectories("ab*"),那么全部以ab开头的目录会被显示
DataRow Dr=tb.NewRow();
Dr["name"] = dChild.Name; //打印目录名
Dr["fullname"] = dChild.FullName; //打印路径和目录名
tb.Rows.Add(Dr);
}
return tb;
}
//遍历一个目录下的全部目录
public static DataTable GetFile(string FilePath)
{
DataTable tb = new DataTable();
tb.Columns.Add("name", typeof(string));
tb.Columns.Add("fullname", typeof(string));
string strPath = System.Web.HttpContext.Current.Server.MapPath(FilePath).ToString();
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(strPath);
foreach (System.IO.FileInfo dChild in dir.GetFiles("*"))
{
//如果用GetDirectories("ab*"),那么全部以ab开头的目录会被显示
DataRow Dr = tb.NewRow();
Dr["name"] = dChild.Name; //打印目录名
Dr["fullname"] = dChild.FullName; //打印路径和目录名
tb.Rows.Add(Dr);
}
return tb;
}
分享到:
相关推荐
简单的filestream的应用实例,说明了他对文件的读写过程。
《C#开发实例大全(基础卷)》筛选、汇集了C#开发从基础知识到高级应用各个层面约600个实例及源代码,每个实例都按实例说明、关键技术、设计过程、详尽注释、秘笈心法的顺序进行了分析解读。全书分6篇共25章,主要...
详细介绍了C#基本文件读写语法 在winform下进行文本文件txt的读写 界面清晰简单 适合入门读者
FileStream fs = new FileStream(@c:\temp\test.txt , FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); String line=sr.ReadLine(); while (line!=null) { Console.WriteLine...
本文实例讲述了C#使用FileStream循环读取大文件数据的方法。分享给大家供大家参考,具体如下: 今天学习了FileStream的用法,用来读取文件流,教程上都是读取小文件,一次性读取,但是如果遇到大文件,那么我们就...
实例221 使用FileStream复制大文件 308 实例222 复制文件时显示复制进度 310 实例223 批量复制文件 312 6.6 指定类型的文件操作 313 实例224 文本文件的操作 313 实例225 使用ROT13加密解密文件 314 6.7 其他 ...
实例221 使用FileStream复制大文件 308 实例222 复制文件时显示复制进度 310 实例223 批量复制文件 312 6.6 指定类型的文件操作 313 实例224 文本文件的操作 313 实例225 使用ROT13加密解密文件 314 6.7 其他 ...
实例221 使用FileStream复制大文件 308 实例222 复制文件时显示复制进度 310 实例223 批量复制文件 312 6.6 指定类型的文件操作 313 实例224 文本文件的操作 313 实例225 使用ROT13加密解密文件 314 6.7 其他 ...
实例221 使用FileStream复制大文件 308 实例222 复制文件时显示复制进度 310 实例223 批量复制文件 312 6.6 指定类型的文件操作 313 实例224 文本文件的操作 313 实例225 使用ROT13加密解密文件 314 6.7 其他 ...