asp.net
using System;
using System.IO;
using System.Web;
namespace SEC
{
/**//// <summary>
/// 對檔案和檔案夾的操作類
/// </summary>
public class FileControl
{
public FileControl()
{
}
/**//// <summary>
/// 在根目錄下建立檔案夾
/// </summary>
/// <param name="FolderPath">要建立的檔案路徑</param>
public void CreateFolder(string FolderPathName)
{
if(FolderPathName.Trim().Length>0)
{
try
{
string CreatePath = System.Web.HttpContext.Current.Server.MapPath
("../../../Images/"+FolderPathName).ToString();
if(!Directory.Exists(CreatePath))
{
Directory.CreateDirectory(CreatePath);
}
}
catch
{
throw;
}
}
}
/**//// <summary>
/// 刪除一個檔案夾下面的字檔案夾和檔案
/// </summary>
/// <param name="FolderPathName"></param>
public void DeleteChildFolder(string FolderPathName)
{
if(FolderPathName.Trim().Length>0)
{
try
{
string CreatePath = System.Web.HttpContext.Current.Server.MapPath
(FolderPathName).ToString();
if(Directory.Exists(CreatePath))
{
Directory.Delete(CreatePath,true);
}
}
catch
{
throw;
}
}
}
/**//// <summary>
/// 刪除一個檔案
/// </summary>
/// <param name="FilePathName"></param>
public void DeleteFile(string FilePathName)
{
try
{
FileInfo DeleFile = new FileInfo(System.Web.HttpContext.Current.Server.MapPath
(FilePathName).ToString());
DeleFile.Delete();
}
catch
{
}
}
public void CreateFile(string FilePathName)
{
try
{
//建立檔案夾
string[] strPath= FilePathName.Split('/');
CreateFolder(FilePathName.Replace("/" + strPath[strPath.Length-1].ToString(),"")); //建立檔案
夾
FileInfo CreateFile =new FileInfo(System.Web.HttpContext.Current.Server.MapPath
(FilePathName).ToString()); //建立檔案
if(!CreateFile.Exists)
{
FileStream FS=CreateFile.Create();
FS.Close();
}
}
catch
{
}
}
/**//// <summary>
/// 刪除整個檔案夾及其字檔案夾和檔案
/// </summary>
/// <param name="FolderPathName"></param>
public void DeleParentFolder(string FolderPathName)
{
try
{
DirectoryInfo DelFolder = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath
(FolderPathName).ToString());
if(DelFolder.Exists)
{
DelFolder.Delete();
}
}
catch
{
}
}
/**//// <summary>
/// 在檔案裡追加內容
/// </summary>
/// <param name="FilePathName"></param>
public void ReWriteReadinnerText(string FilePathName,string WriteWord)
{
try
{
//建立檔案夾和檔案
//CreateFolder(FilePathName);
CreateFile(FilePathName);
//得到原來檔案的內容
FileStream FileRead=new FileStream(System.Web.HttpContext.Current.Server.MapPath
(FilePathName).ToString(),FileMode.Open,FileAccess.ReadWrite);
StreamReader FileReadWord=new StreamReader(FileRead,System.Text.Encoding.Default);
string OldString = FileReadWord.ReadToEnd().ToString();
OldString = OldString + WriteWord;
//把新的內容重新寫入
StreamWriter FileWrite=new StreamWriter(FileRead,System.Text.Encoding.Default);
FileWrite.Write(WriteWord);
//關閉
FileWrite.Close();
FileReadWord.Close();
FileRead.Close();
}
catch
{
// throw;
}
}
/**//// <summary>
/// 在檔案裡追加內容
/// </summary>
/// <param name="FilePathName"></param>
public string ReaderFileData(string FilePathName)
{
try
{
FileStream FileRead=new FileStream(System.Web.HttpContext.Current.Server.MapPath
(FilePathName).ToString(),FileMode.Open,FileAccess.Read);
StreamReader FileReadWord=new StreamReader(FileRead,System.Text.Encoding.Default);
string TxtString = FileReadWord.ReadToEnd().ToString();
//關閉
FileReadWord.Close();
FileRead.Close();
return TxtString;
}
catch
{
throw;
}
}
/**//// <summary>
/// 讀取檔案夾的檔案
/// </summary>
/// <param name="FilePathName"></param>
/// <returns></returns>
public DirectoryInfo checkValidSessionPath(string FilePathName)
{
try
{
DirectoryInfo MainDir = new DirectoryInfo(System.Web.HttpContext.Current.Server.MapPath
(FilePathName));
return MainDir;
}
catch
{
throw;
}
}
}
}