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;
using System.Text;
using System.IO;
namespace EC
{
/// <summary>
/// FileObj 的摘要說明
/// </summary>
public class FileObj
{
#region 構造函數(shù)
private bool _alreadyDispose = false;
public FileObj()
{
//
// TODO: 在此處添加構造函數(shù)邏輯
//
}
~FileObj()
{
Dispose(); ;
}
protected virtual void Dispose(bool isDisposing)
{
if (_alreadyDispose) return;
//if (isDisposing)
//{
// if (xml != null)
// {
// xml = null;
// }
//}
_alreadyDispose = true;
}
#endregion
#region IDisposable 成員
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
#region 取得文件后綴名
/****************************************
* 函數(shù)名稱:GetPostfixStr
* 功能說明:取得文件后綴名
* 參 數(shù):filename:文件名稱
* 調用示列:
* string filename = "aaa.aspx";
* string s = EC.FileObj.GetPostfixStr(filename);
*****************************************/
/// <summary>
/// 取后綴名
/// </summary>
/// <param name="filename">文件名</param>
/// <returns>.gif|.html格式</returns>
public static string GetPostfixStr(string filename)
{
int start = filename.LastIndexOf(".");
int length = filename.Length;
string postfix = filename.Substring(start, length - start);
return postfix;
}
#endregion
#region 寫文件
/****************************************
* 函數(shù)名稱:WriteFile
* 功能說明:當文件不存時,則創(chuàng)建文件,并追加文件
* 參 數(shù):Path:文件路徑,Strings:文本內容
* 調用示列:
* string Path = Server.MapPath("Default2.aspx");
* string Strings = "這是我寫的內容啊";
* EC.FileObj.WriteFile(Path,Strings);
*****************************************/
/// <summary>
/// 寫文件
/// </summary>
/// <param name="Path">文件路徑</param>
/// <param name="Strings">文件內容</param>
public static void WriteFile(string Path, string Strings)
{
if (!System.IO.File.Exists(Path))
{
//Directory.CreateDirectory(Path);
System.IO.FileStream f = System.IO.File.Create(Path);
f.Close();
f.Dispose();
}
System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.UTF8);
f2.WriteLine(Strings);
f2.Close();
f2.Dispose();
}
#endregion
#region 讀文件
/****************************************
* 函數(shù)名稱:ReadFile
* 功能說明:讀取文本內容
* 參 數(shù):Path:文件路徑
* 調用示列:
* string Path = Server.MapPath("Default2.aspx");
* string s = EC.FileObj.ReadFile(Path);
*****************************************/
/// <summary>
/// 讀文件
/// </summary>
/// <param name="Path">文件路徑</param>
/// <returns></returns>
public static string ReadFile(string Path)
{
string s = "";
if (!System.IO.File.Exists(Path))
s = "不存在相應的目錄";
else
{
StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312"));
s = f2.ReadToEnd();
f2.Close();
f2.Dispose();
}
return s;
}
#endregion
#region 追加文件
/****************************************
* 函數(shù)名稱:FileAdd
* 功能說明:追加文件內容
* 參 數(shù):Path:文件路徑,strings:內容
* 調用示列:
* string Path = Server.MapPath("Default2.aspx");
* string Strings = "新追加內容";
* EC.FileObj.FileAdd(Path, Strings);
*****************************************/
/// <summary>
/// 追加文件
/// </summary>
/// <param name="Path">文件路徑</param>
/// <param name="strings">內容</param>
public static void FileAdd(string Path, string strings)
{
StreamWriter sw = File.AppendText(Path);
sw.Write(strings);
sw.Flush();
sw.Close();
sw.Dispose();
}
#endregion
#region 拷貝文件
/****************************************
* 函數(shù)名稱:FileCoppy
* 功能說明:拷貝文件
* 參 數(shù):OrignFile:原始文件,NewFile:新文件路徑
* 調用示列:
* string OrignFile = Server.MapPath("Default2.aspx");
* string NewFile = Server.MapPath("Default3.aspx");
* EC.FileObj.FileCoppy(OrignFile, NewFile);
*****************************************/
/// <summary>
/// 拷貝文件
/// </summary>
/// <param name="OrignFile">原始文件</param>
/// <param name="NewFile">新文件路徑</param>
public static void FileCoppy(string OrignFile, string NewFile)
{
File.Copy(OrignFile, NewFile, true);
}
#endregion
#region 刪除文件
/****************************************
* 函數(shù)名稱:FileDel
* 功能說明:刪除文件
* 參 數(shù):Path:文件路徑
* 調用示列:
* string Path = Server.MapPath("Default3.aspx");
* EC.FileObj.FileDel(Path);
*****************************************/
/// <summary>
/// 刪除文件
/// </summary>
/// <param name="Path">路徑</param>
public static void FileDel(string Path)
{
File.Delete(Path);
}
#endregion
#region 移動文件
/****************************************
* 函數(shù)名稱:FileMove
* 功能說明:移動文件
* 參 數(shù):OrignFile:原始路徑,NewFile:新文件路徑
* 調用示列:
* string OrignFile = Server.MapPath("../說明.txt");
* string NewFile = Server.MapPath("../../說明.txt");
* EC.FileObj.FileMove(OrignFile, NewFile);
*****************************************/
/// <summary>
/// 移動文件
/// </summary>
/// <param name="OrignFile">原始路徑</param>
/// <param name="NewFile">新路徑</param>
public static void FileMove(string OrignFile, string NewFile)
{
File.Move(OrignFile, NewFile);
}
#endregion
#region 在當前目錄下創(chuàng)建目錄
/****************************************
* 函數(shù)名稱:FolderCreate
* 功能說明:在當前目錄下創(chuàng)建目錄
* 參 數(shù):OrignFolder:當前目錄,NewFloder:新目錄
* 調用示列:
* string OrignFolder = Server.MapPath("test/");
* string NewFloder = "new";
* EC.FileObj.FolderCreate(OrignFolder, NewFloder);
*****************************************/
/// <summary>
/// 在當前目錄下創(chuàng)建目錄
/// </summary>
/// <param name="OrignFolder">當前目錄</param>
/// <param name="NewFloder">新目錄</param>
public static void FolderCreate(string OrignFolder, string NewFloder)
{
Directory.SetCurrentDirectory(OrignFolder);
Directory.CreateDirectory(NewFloder);
}
/// <summary>
/// 創(chuàng)建文件夾
/// </summary>
/// <param name="Path"></param>
public static void FolderCreate(string Path)
{
// 判斷目標目錄是否存在如果不存在則新建之
if (!Directory.Exists(Path))
Directory.CreateDirectory(Path);
}
#endregion
#region 創(chuàng)建目錄
public static void FileCreate(string Path)
{
FileInfo CreateFile = new FileInfo(Path); //創(chuàng)建文件
if (!CreateFile.Exists)
{
FileStream FS = CreateFile.Create();
FS.Close();
}
}
#endregion
#region 遞歸刪除文件夾目錄及文件
/****************************************
* 函數(shù)名稱:DeleteFolder
* 功能說明:遞歸刪除文件夾目錄及文件
* 參 數(shù):dir:文件夾路徑
* 調用示列:
* string dir = Server.MapPath("test/");
* EC.FileObj.DeleteFolder(dir);
*****************************************/
/// <summary>
/// 遞歸刪除文件夾目錄及文件
/// </summary>
/// <param name="dir"></param>
/// <returns></returns>
public static void DeleteFolder(string dir)
{
if (Directory.Exists(dir)) //如果存在這個文件夾刪除之
{
foreach (string d in Directory.GetFileSystemEntries(dir))
{
if (File.Exists(d))
File.Delete(d); //直接刪除其中的文件
else
DeleteFolder(d); //遞歸刪除子文件夾
}
Directory.Delete(dir, true); //刪除已空文件夾
}
}
#endregion
#region 將指定文件夾下面的所有內容copy到目標文件夾下面 果目標文件夾為只讀屬性就會報錯。
/****************************************
* 函數(shù)名稱:CopyDir
* 功能說明:將指定文件夾下面的所有內容copy到目標文件夾下面 果目標文件夾為只讀屬性就會報錯。
* 參 數(shù):srcPath:原始路徑,aimPath:目標文件夾
* 調用示列:
* string srcPath = Server.MapPath("test/");
* string aimPath = Server.MapPath("test1/");
* EC.FileObj.CopyDir(srcPath,aimPath);
*****************************************/
/// <summary>
/// 指定文件夾下面的所有內容copy到目標文件夾下面
/// </summary>
/// <param name="srcPath">原始路徑</param>
/// <param name="aimPath">目標文件夾</param>
public static void CopyDir(string srcPath, string aimPath)
{
try
{
// 檢查目標目錄是否以目錄分割字符結束如果不是則添加之
if (aimPath[aimPath.Length - 1] != Path.DirectorySeparatorChar)
aimPath += Path.DirectorySeparatorChar;
// 判斷目標目錄是否存在如果不存在則新建之
if (!Directory.Exists(aimPath))
Directory.CreateDirectory(aimPath);
// 得到源目錄的文件列表,該里面是包含文件以及目錄路徑的一個數(shù)組
//如果你指向copy目標文件下面的文件而不包含目錄請使用下面的方法
//string[] fileList = Directory.GetFiles(srcPath);
string[] fileList = Directory.GetFileSystemEntries(srcPath);
//遍歷所有的文件和目錄
foreach (string file in fileList)
{
//先當作目錄處理如果存在這個目錄就遞歸Copy該目錄下面的文件
if (Directory.Exists(file))
CopyDir(file, aimPath + Path.GetFileName(file));
//否則直接Copy文件
else
File.Copy(file, aimPath + Path.GetFileName(file), true);
}
}
catch (Exception ee)
{
throw new Exception(ee.ToString());
}
}
#endregion
#region 獲取指定文件夾下所有子目錄及文件(樹形)
/****************************************
* 函數(shù)名稱:GetFoldAll(string Path)
* 功能說明:獲取指定文件夾下所有子目錄及文件(樹形)
* 參 數(shù):Path:詳細路徑
* 調用示列:
* string strDirlist = Server.MapPath("templates");
* this.Literal1.Text = EC.FileObj.GetFoldAll(strDirlist);
*****************************************/
/// <summary>
/// 獲取指定文件夾下所有子目錄及文件
/// </summary>
/// <param name="Path">詳細路徑</param>
public static string GetFoldAll(string Path)
{
string str = "";
DirectoryInfo thisOne = new DirectoryInfo(Path);
str = ListTreeShow(thisOne, 0, str);
return str;
}
/// <summary>
/// 獲取指定文件夾下所有子目錄及文件函數(shù)
/// </summary>
/// <param name="theDir">指定目錄</param>
/// <param name="nLevel">默認起始值,調用時,一般為0</param>
/// <param name="Rn">用于迭加的傳入值,一般為空</param>
/// <returns></returns>
public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn)//遞歸目錄 文件
{
DirectoryInfo[] subDirectories = theDir.GetDirectories();//獲得目錄
foreach (DirectoryInfo dirinfo in subDirectories)
{
if (nLevel == 0)
{
Rn += "├";
}
else
{
string _s = "";
for (int i = 1; i <= nLevel; i++)
{
_s += "│ ";
}
Rn += _s + "├";
}
Rn += "<b>" + dirinfo.Name.ToString() + "</b><br />";
FileInfo[] fileInfo = dirinfo.GetFiles(); //目錄下的文件
foreach (FileInfo fInfo in fileInfo)
{
if (nLevel == 0)
{
Rn += "│ ├";
}
else
{
string _f = "";
for (int i = 1; i <= nLevel; i++)
{
_f += "│ ";
}
Rn += _f + "│ ├";
}
Rn += fInfo.Name.ToString() + " <br />";
}
Rn = ListTreeShow(dirinfo, nLevel + 1, Rn);
}
return Rn;
}
/****************************************
* 函數(shù)名稱:GetFoldAll(string Path)
* 功能說明:獲取指定文件夾下所有子目錄及文件(下拉框形)
* 參 數(shù):Path:詳細路徑
* 調用示列:
* string strDirlist = Server.MapPath("templates");
* this.Literal2.Text = EC.FileObj.GetFoldAll(strDirlist,"tpl","");
*****************************************/
/// <summary>
/// 獲取指定文件夾下所有子目錄及文件(下拉框形)
/// </summary>
/// <param name="Path">詳細路徑</param>
///<param name="DropName">下拉列表名稱</param>
///<param name="tplPath">默認選擇模板名稱</param>
public static string GetFoldAll(string Path,string DropName,string tplPath)
{
string strDrop = "<select name=\"" + DropName + "\" id=\"" + DropName + "\"><option value=\"\">--請選擇詳細模板--</option>";
string str = "";
DirectoryInfo thisOne = new DirectoryInfo(Path);
str = ListTreeShow(thisOne, 0, str,tplPath);
return strDrop+str+"</select>";
}
/// <summary>
/// 獲取指定文件夾下所有子目錄及文件函數(shù)
/// </summary>
/// <param name="theDir">指定目錄</param>
/// <param name="nLevel">默認起始值,調用時,一般為0</param>
/// <param name="Rn">用于迭加的傳入值,一般為空</param>
/// <param name="tplPath">默認選擇模板名稱</param>
/// <returns></returns>
public static string ListTreeShow(DirectoryInfo theDir, int nLevel, string Rn,string tplPath)//遞歸目錄 文件
{
DirectoryInfo[] subDirectories = theDir.GetDirectories();//獲得目錄
foreach (DirectoryInfo dirinfo in subDirectories)
{
Rn += "<option value=\"" + dirinfo.Name.ToString() + "\"";
if (tplPath.ToLower() == dirinfo.Name.ToString().ToLower())
{
Rn += " selected ";
}
Rn += ">";
if (nLevel == 0)
{
Rn += "┣";
}
else
{
string _s = "";
for (int i = 1; i <= nLevel; i++)
{
_s += "│ ";
}
Rn += _s + "┣";
}
Rn += "" + dirinfo.Name.ToString() + "</option>";
FileInfo[] fileInfo = dirinfo.GetFiles(); //目錄下的文件
foreach (FileInfo fInfo in fileInfo)
{
Rn += "<option value=\"" + dirinfo.Name.ToString()+"/"+fInfo.Name.ToString() + "\"";
if (tplPath.ToLower() == fInfo.Name.ToString().ToLower())
{
Rn += " selected ";
}
Rn += ">";
if (nLevel == 0)
{
Rn += "│ ├";
}
else
{
string _f = "";
for (int i = 1; i <= nLevel; i++)
{
_f += "│ ";
}
Rn += _f + "│ ├";
}
Rn += fInfo.Name.ToString() + "</option>";
}
Rn = ListTreeShow(dirinfo, nLevel + 1, Rn, tplPath);
}
return Rn;
}
#endregion
#region 獲取文件夾大小
/****************************************
* 函數(shù)名稱:GetDirectoryLength(string dirPath)
* 功能說明:獲取文件夾大小
* 參 數(shù):dirPath:文件夾詳細路徑
* 調用示列:
* string Path = Server.MapPath("templates");
* Response.Write(EC.FileObj.GetDirectoryLength(Path));
*****************************************/
/// <summary>
/// 獲取文件夾大小
/// </summary>
/// <param name="dirPath">文件夾路徑</param>
/// <returns></returns>
public static long GetDirectoryLength(string dirPath)
{
if (!Directory.Exists(dirPath))
return 0;
long len = 0;
DirectoryInfo di = new DirectoryInfo(dirPath);
foreach (FileInfo fi in di.GetFiles())
{
len += fi.Length;
}
DirectoryInfo[] dis = di.GetDirectories();
if (dis.Length > 0)
{
for (int i = 0; i < dis.Length; i++)
{
len += GetDirectoryLength(dis[i].FullName);
}
}
return len;
}
#endregion
#region 獲取指定文件詳細屬性
/****************************************
* 函數(shù)名稱:GetFileAttibe(string filePath)
* 功能說明:獲取指定文件詳細屬性
* 參 數(shù):filePath:文件詳細路徑
* 調用示列:
* string file = Server.MapPath("robots.txt");
* Response.Write(EC.FileObj.GetFileAttibe(file));
*****************************************/
/// <summary>
/// 獲取指定文件詳細屬性
/// </summary>
/// <param name="filePath">文件詳細路徑</param>
/// <returns></returns>
public static string GetFileAttibe(string filePath)
{
string str = "";
System.IO.FileInfo objFI = new System.IO.FileInfo(filePath);
str += "詳細路徑:" + objFI.FullName + "<br>文件名稱:" + objFI.Name + "<br>文件長度:" + objFI.Length.ToString() + "字節(jié)<br>創(chuàng)建時間" + objFI.CreationTime.ToString() + "<br>最后訪問時間:" + objFI.LastAccessTime.ToString() + "<br>修改時間:" + objFI.LastWriteTime.ToString() + "<br>所在目錄:" + objFI.DirectoryName + "<br>擴展名:" + objFI.Extension;
return str;
}
#endregion
}
}