/// <summary>
/// 本类说明:用来处理fso的操作!
/// </summary>
public partial class ReadTempleContent
{
#region 用来读取指定的文件
/// <summary>
/// 本函数用来读取文件的内容
/// </summary>
/// <param name="StrPath">文件的路径,相对于服务器的路径</param>
/// <returns></returns>
public static string ReadContent(string StrPath)
{
try
{
string StrServerPath = HttpContext.Current.Server.MapPath(StrPath);
if (File.Exists(StrServerPath))
{
return File.ReadAllText(StrServerPath, Encoding.GetEncoding("gb2312"));
}
else
{
return "";
}
}
catch
{
return "";
}
}
#endregion
#region 用来生成文件
/// <summary>
/// 用来生成新的文件
/// </summary>
/// <param name="StrContent">要生成的文件的内容</param>
/// <param name="StrNewPath">生成文件保存的位置</param>
/// <returns>true 表示生成成功,false生成失败</returns>
public static bool CreatNew(string StrContent, string StrNewPath)
{
try
{
string StrServerPath = HttpContext.Current.Server.MapPath(StrNewPath);
if (File.Exists(StrServerPath))
{
File.Delete(StrServerPath);
}
FileStream FS = File.Create(StrServerPath);
byte[] ContentByte = Encoding.GetEncoding("gb2312").GetBytes(StrContent);
FS.Write(ContentByte, 0, ContentByte.Length);
FS.Close();
FS = null;
return true;
}
catch
{
return false;
}
}
#endregion
#region 向文件里追加文本
/// <summary>
/// 向文件里追加文本
/// </summary>
/// <param name="StrAddContent">要追加的文本</param>
/// <param name="StrPath">文件的路径</param>
/// <returns></returns>
public static bool AddText(string StrAddContent, string StrPath)
{
try
{
string StrServerPath = HttpContext.Current.Server.MapPath(StrPath);
File.AppendAllText(StrServerPath, StrAddContent);
return true;
}
catch
{
return false;
}
}
#endregion
#region 删除指定的文件
/// <summary>
/// 删除指定路径的文件
/// </summary>
/// <param name="StrPath">指定文件的路径</param>
/// <returns></returns>
public static bool DeleteFile(string StrPath)
{
try
{
string StrServerPath = HttpContext.Current.Server.MapPath(StrPath);
if (File.Exists(StrServerPath))
{
File.Delete(StrServerPath);
}
return true;
}
catch
{
return false;//删除数据失败!
}
}
#endregion
#region 用来创建文件夹
/// <summary>
/// 用来创建文件夹
/// </summary>
/// <param name="FolderPath">文件路径</param>
/// <returns></returns>
public static string CreatFolder(string FolderPath,string cid)
{
try
{
string StrFolderServerPath = HttpContext.Current.Server.MapPath(FolderPath);
if (!Directory.Exists(StrFolderServerPath))
{
Directory.CreateDirectory(StrFolderServerPath);
}
string StrFolderServerPathc = HttpContext.Current.Server.MapPath( FolderPath+"/"+cid );
if ( !Directory.Exists( StrFolderServerPathc ) )
{
Directory.CreateDirectory( StrFolderServerPathc );
}
return FolderPath+"/"+cid+"/";
}
catch
{
return "";
}
}
#endregion
#region 用来删除整个文件夹,及其文件夹的内容
/// <summary>
/// 用来删除文件夹
/// </summary>
/// <param name="FolderName">要删除的文件夹的名字</param>
/// <returns></returns>
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;
}
}
else
{
return false;
}
}
#endregion
#region 删除文件
/// <summary>
/// 删除父文件夹
/// </summary>
/// <param name="FolderPathName">要删除的文件夹的路径名</param>
/// <returns></returns>
public static bool DeleParentFolder(string FolderPathName)
{
try
{
string strPath = HttpContext.Current.Server.MapPath(FolderPathName);
DirectoryInfo DelFolder = new DirectoryInfo(strPath);
if (DelFolder.Exists)
{
DelFolder.Delete();
}
return true;
}
catch
{
return false;
}
}
#endregion
#region 用来得到一个目录下的所有目录的
public static DataTable GetDir(string FilePath)
{
DataTable tb = new DataTable();
tb.Columns.Add("name", typeof(string));
tb.Columns.Add("fullname", typeof(string));
string strPath = HttpContext.Current.Server.MapPath(FilePath);
DirectoryInfo dir = new DirectoryInfo(strPath);
foreach (DirectoryInfo dChild in dir.GetDirectories("*"))
{
//如果用GetDirectories("ab*"),那么全部以ab开头的目录会被显示
DataRow Dr = tb.NewRow();
Dr["name"] = dChild.Name; //打印目录名
Dr["fullname"] = FilePath+"/"+dChild.Name; //打印路径和目录名
tb.Rows.Add(Dr);
}
return tb;
}
#endregion
#region 用来遍历一个目录下的所有文件并找到对应文件后删除!
public static void GetDeleteFile(string FilePath,string filename)
{
string strPath = HttpContext.Current.Server.MapPath(FilePath);
DirectoryInfo dir = new DirectoryInfo(strPath);
foreach (DirectoryInfo dChild in dir.GetDirectories("*"))
{
GetDeleteFile(FilePath + "/" + dChild.Name, filename);
if (dChild.Name == filename)
{
DeleteFolder(FilePath + "/" + dChild.ToString());
}
}
}
#endregion
}