123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648 |
- using System;
- using System.Text;
- using System.Web;
- using System.IO;
- namespace Ant.Service.Common
- {
- public class FileOperate
- {
- #region 文件删除
-
-
-
-
- public static void DelFile(string FileRealPath)
- {
- if (File.Exists(FileRealPath))
- File.Delete(FileRealPath);
- }
-
-
-
-
- public static void FileDelete(string VirtualFilePath)
- {
-
- string RealFilePath = HttpContext.Current.Server.MapPath(VirtualFilePath);
-
- if (File.Exists(VirtualFilePath))
- {
- File.Delete(VirtualFilePath);
- }
- }
-
-
-
-
-
- public static void FileDelete(string VirtualFilePath, bool IsDeleteForAll)
- {
-
- string RealFilePath = HttpContext.Current.Server.MapPath(VirtualFilePath);
- DelFile(RealFilePath);
-
- if (IsDeleteForAll)
- {
-
- string FileName = Path.GetFileName(RealFilePath);
-
- string FilePath = Path.GetDirectoryName(RealFilePath);
-
- string SmallPath = FilePath + "/small/" + FileName;
-
- string MiddlePath = FilePath + "/middle/" + FileName;
- DelFile(SmallPath);
- DelFile(MiddlePath);
- }
- }
- #endregion
- #region 写文件
- protected void Write_Txt(string FileName, string Content)
- {
- Encoding code = Encoding.GetEncoding("gb2312");
- string htmlfilename = HttpContext.Current.Server.MapPath("Precious\\" + FileName + ".txt");
- 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);
- str = sr.ReadToEnd();
- }
- 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)
- {
- if (File.Exists(Path))
- File.Delete(Path);
- }
- #endregion
- #region 移动文件
-
-
-
-
-
-
- public static void FileMove(string OrignFile, string NewFile)
- {
- File.Move(OrignFile, NewFile);
- }
- #endregion
- #region 在当前目录下创建目录
-
-
-
-
-
-
- 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 创建目录
- public static void FileCreate(string Path)
- {
- FileInfo CreateFile = new FileInfo(Path);
- if (!CreateFile.Exists)
- {
- FileStream FS = CreateFile.Create();
- FS.Close();
- }
- }
- #endregion
- #region 递归删除文件夹目录及文件
-
-
-
-
-
-
- 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到目标文件夹下面 果目标文件夹为只读属性就会报错。
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
- 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;
- }
-
-
-
-
-
-
-
- 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>";
- }
-
-
-
-
-
-
-
-
- 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 获取文件夹大小
-
-
-
-
-
-
- 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() + "字节<br>创建时间" + objFI.CreationTime.ToString() + "<br>最后访问时间:" + objFI.LastAccessTime.ToString() + "<br>修改时间:" + objFI.LastWriteTime.ToString() + "<br>所在目录:" + objFI.DirectoryName + "<br>扩展名:" + objFI.Extension;
- return str;
- }
- #endregion
- }
- }
|