C#封裝的常用文件操作源碼類

如下代碼段是關(guān)于C#封裝的常用文件操作類的代碼浪蹂,希望對小伙伴們有些用處肥卡。

using System;

using System.Text;

using System.Web;

using System.IO;

namespace DotNet.Utilities

{

? ? public class FileOperate

? ? {

? ? ? ? #region 寫文件

? ? ? ? protected void Write_Txt(string FileName, string Content)

? ? ? ? {

? ? ? ? ? ? Encoding code = Encoding.GetEncoding("gb2312");

? ? ? ? ? ? string str = Content;

? ? ? ? ? ? StreamWriter sw = null;

? ? ? ? ? ? {

? ? ? ? ? ? ? ? try

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? sw = new StreamWriter(htmlfilename, false, code);

? ? ? ? ? ? ? ? ? ? sw.Write(str);

? ? ? ? ? ? ? ? ? ? sw.Flush();

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? catch { }

? ? ? ? ? ? }

? ? ? ? ? ? sw.Close();

? ? ? ? ? ? sw.Dispose();

? ? ? ? }

? ? ? ? #endregion

? ? ? ? #region 讀文件

? ? ? ? protected string Read_Txt(string filename)

? ? ? ? {

? ? ? ? ? ? Encoding code = Encoding.GetEncoding("gb2312");

? ? ? ? ? ? string temp = HttpContext.Current.Server.MapPath("Precious\" + filename + ".txt");

? ? ? ? ? ? string str = "";

? ? ? ? ? ? if (File.Exists(temp))

? ? ? ? ? ? {

? ? ? ? ? ? ? ? StreamReader sr = null;

? ? ? ? ? ? ? ? try

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? sr = new StreamReader(temp, code);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? catch { }

? ? ? ? ? ? ? ? sr.Close();

? ? ? ? ? ? ? ? sr.Dispose();

? ? ? ? ? ? }

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? str = "";

? ? ? ? ? ? }

? ? ? ? ? ? return str;

? ? ? ? }

? ? ? ? #endregion

? ? ? ? #region 取得文件后綴名

? ? ? ? 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 寫文件

? ? ? ? public static void WriteFile(string Path, string Strings)

? ? ? ? {

? ? ? ? ? ? if (!System.IO.File.Exists(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 讀文件

? ? ? ? public static string ReadFile(string Path)

? ? ? ? {

? ? ? ? ? ? string s = "";

? ? ? ? ? ? if (!System.IO.File.Exists(Path))

? ? ? ? ? ? ? ? s = "不存在相應(yīng)的目錄";

? ? ? ? ? ? else

? ? ? ? ? ? {

? ? ? ? ? ? ? ? StreamReader f2 = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312"));

? ? ? ? ? ? ? ? s = f2.ReadToEnd();

? ? ? ? ? ? ? ? f2.Close();

? ? ? ? ? ? ? ? f2.Dispose();

? ? ? ? ? ? }

? ? ? ? ? ? return s;

? ? ? ? }

? ? ? ? #endregion

? ? ? ? #region 追加文件

? ? ? ? public static void FileAdd(string Path, string strings)

? ? ? ? {

? ? ? ? ? ? StreamWriter sw = File.AppendText(Path);

? ? ? ? ? ? sw.Write(strings);

? ? ? ? ? ? sw.Flush();

? ? ? ? ? ? sw.Close();

? ? ? ? ? ? sw.Dispose();

? ? ? ? }

? ? ? ? #endregion

? ? ? ? #region 拷貝文件

? ? ? ? public static void FileCoppy(string OrignFile, string NewFile)

? ? ? ? {

? ? ? ? ? ? File.Copy(OrignFile, NewFile, true);

? ? ? ? }

? ? ? ? #endregion

? ? ? ? #region 刪除文件

? ? ? ? public static void FileDel(string Path)

? ? ? ? {

? ? ? ? ? ? File.Delete(Path);

? ? ? ? }

? ? ? ? #endregion

? ? ? ? #region 移動文件

? ? ? ? public static void FileMove(string OrignFile, string NewFile)

? ? ? ? {

? ? ? ? ? ? File.Move(OrignFile, NewFile);

? ? ? ? }

? ? ? ? #endregion

? ? ? ? #region 在當(dāng)前目錄下創(chuàng)建目錄

? ? ? ? public static void FolderCreate(string OrignFolder, string NewFloder)

? ? ? ? {

? ? ? ? ? ? Directory.SetCurrentDirectory(OrignFolder);

? ? ? ? ? ? Directory.CreateDirectory(NewFloder);

? ? ? ? }

? ? ? ? public static void FolderCreate(string Path)

? ? ? ? {

? ? ? ? ? ? if (!Directory.Exists(Path))

? ? ? ? ? ? ? ? Directory.CreateDirectory(Path);

? ? ? ? }

? ? ? ? #endregion

? ? ? ? #region 創(chuàng)建目錄

? ? ? ? public static void FileCreate(string Path)

? ? ? ? {

? ? ? ? ? ? if (!CreateFile.Exists)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? FileStream FS = CreateFile.Create();

? ? ? ? ? ? ? ? FS.Close();

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? #endregion

? ? ? ? #region 遞歸刪除文件夾目錄及文件

? ? ? ? public static void DeleteFolder(string dir)

? ? ? ? {

? ? ? ? ? ? {

? ? ? ? ? ? ? ? foreach (string d in Directory.GetFileSystemEntries(dir))

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? if (File.Exists(d))

? ? ? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? #endregion

? ? ? ? #region 將指定文件夾下面的所有內(nèi)容copy到目標(biāo)文件夾下面 果目標(biāo)文件夾為只讀屬性就會報錯溪掀。

? ? ? ? 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);

? ? ? ? ? ? ? ? string[] fileList = Directory.GetFileSystemEntries(srcPath);

? ? ? ? ? ? ? ? foreach (string file in fileList)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? if (Directory.Exists(file))

? ? ? ? ? ? ? ? ? ? ? ? CopyDir(file, aimPath + Path.GetFileName(file));

? ? ? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? ? ? ? ? File.Copy(file, aimPath + Path.GetFileName(file), true);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? catch (Exception ee)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? throw new Exception(ee.ToString());

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? #endregion

? ? ? ? #region 獲取指定文件夾下所有子目錄及文件(樹形)

? ? ? ? public static string GetFoldAll(string Path)

? ? ? ? {

? ? ? ? ? ? string str = "";

? ? ? ? ? ? DirectoryInfo thisOne = new DirectoryInfo(Path);

? ? ? ? ? ? str = ListTreeShow(thisOne, 0, str);

? ? ? ? ? ? return str;

? ? ? ? }

? ? ? ? {

? ? ? ? ? ? foreach (DirectoryInfo dirinfo in subDirectories)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? if (nLevel == 0)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? Rn += "├";

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? string _s = "";

? ? ? ? ? ? ? ? ? ? for (int i = 1; i <= nLevel; i++)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? _s += "│&nbsp;";

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? Rn += _s + "├";

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? Rn += "<b>" + dirinfo.Name.ToString() + "</b><br />";

? ? ? ? ? ? ? ? foreach (FileInfo fInfo in fileInfo)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? if (nLevel == 0)

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? Rn += "│&nbsp;├";

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? string _f = "";

? ? ? ? ? ? ? ? ? ? ? ? for (int i = 1; i <= nLevel; i++)

? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? _f += "│&nbsp;";

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? Rn += _f + "│&nbsp;├";

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? Rn += fInfo.Name.ToString() + " <br />";

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? Rn = ListTreeShow(dirinfo, nLevel + 1, Rn);

? ? ? ? ? ? }

? ? ? ? ? ? return Rn;

? ? ? ? }

? ? ? ? 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>";

? ? ? ? }

? ? ? ? {

? ? ? ? ? ? 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 += "│&nbsp;";

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? Rn += _s + "┣";

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? Rn += "" + dirinfo.Name.ToString() + "</option>";

? ? ? ? ? ? ? ? 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 += "│&nbsp;├";

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? else

? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? string _f = "";

? ? ? ? ? ? ? ? ? ? ? ? for (int i = 1; i <= nLevel; i++)

? ? ? ? ? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? ? ? _f += "│&nbsp;";

? ? ? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? ? ? Rn += _f + "│&nbsp;├";

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? ? ? Rn += fInfo.Name.ToString() + "</option>";

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? Rn = ListTreeShow(dirinfo, nLevel + 1, Rn, tplPath);

? ? ? ? ? ? }

? ? ? ? ? ? return Rn;

? ? ? ? }

? ? ? ? #endregion

? ? ? ? #region 獲取文件夾大小

? ? ? ? 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 獲取指定文件詳細屬性

? ? ? ? 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

? ? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市步鉴,隨后出現(xiàn)的幾起案子揪胃,更是在濱河造成了極大的恐慌,老刑警劉巖氛琢,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件喊递,死亡現(xiàn)場離奇詭異,居然都是意外死亡阳似,警方通過查閱死者的電腦和手機骚勘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來撮奏,“玉大人俏讹,你說我怎么就攤上這事⌒蟮酰” “怎么了泽疆?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長定拟。 經(jīng)常有香客問我于微,道長,這世上最難降的妖魔是什么青自? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任株依,我火速辦了婚禮,結(jié)果婚禮上延窜,老公的妹妹穿的比我還像新娘恋腕。我一直安慰自己,他們只是感情好逆瑞,可當(dāng)我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布荠藤。 她就那樣靜靜地躺著,像睡著了一般获高。 火紅的嫁衣襯著肌膚如雪哈肖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天念秧,我揣著相機與錄音淤井,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛币狠,可吹牛的內(nèi)容都是我干的游两。 我是一名探鬼主播,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼漩绵,長吁一口氣:“原來是場噩夢啊……” “哼贱案!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起止吐,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤宝踪,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后碍扔,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體肴沫,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年蕴忆,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片悲幅。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡套鹅,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出汰具,到底是詐尸還是另有隱情卓鹿,我是刑警寧澤,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布留荔,位于F島的核電站吟孙,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏聚蝶。R本人自食惡果不足惜杰妓,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望碘勉。 院中可真熱鬧巷挥,春花似錦、人聲如沸验靡。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽胜嗓。三九已至高职,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間辞州,已是汗流浹背怔锌。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人产禾。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓排作,卻偏偏與公主長得像,于是被迫代替她去往敵國和親亚情。 傳聞我的和親對象是個殘疾皇子妄痪,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容