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

如下代碼段是關于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 = "不存在相應的目錄";

? ? ? ? ? ? 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 在當前目錄下創(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到目標文件夾下面 果目標文件夾為只讀屬性就會報錯。

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

? ? }

}

?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容