123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806 |
- using System;
- using System.Text;
- using System.IO;
- using System.Data;
- using System.Web;
- using System.Reflection;
- namespace WMS.Util
- {
- //string baseDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
- /// <summary>
- /// 描 述:文件夹文件操作类
- /// </summary>
- public static class DirFileUtil
- {
- #region 检测指定目录是否存在
- /// <summary>
- /// 检测指定目录是否存在
- /// </summary>
- /// <param name="directoryPath">目录的绝对路径</param>
- /// <returns></returns>
- public static bool IsExistDirectory(string directoryPath)
- {
- return Directory.Exists(directoryPath);
- }
- #endregion
- #region 获取指定目录中的文件列表
- /// <summary>
- /// 获取指定目录中所有文件列表
- /// </summary>
- /// <param name="directoryPath">指定目录的绝对路径</param>
- public static string[] GetFileNames(string directoryPath)
- {
- //如果目录不存在,则抛出异常
- if (!IsExistDirectory(directoryPath))
- {
- throw new FileNotFoundException();
- }
- //获取文件列表
- return Directory.GetFiles(directoryPath);
- }
- #endregion
- #region 获取指定目录中所有子目录列表
- /// <summary>
- /// 获取指定目录中所有子目录列表
- /// </summary>
- /// <param name="directoryPath">指定目录的绝对路径</param>
- public static string[] GetDirectories(string directoryPath)
- {
- //如果目录不存在,则抛出异常
- if (!IsExistDirectory(directoryPath))
- {
- throw new FileNotFoundException();
- }
- return Directory.GetDirectories(directoryPath);
- }
- #endregion
- #region 获取指定目录及子目录中所有文件列表(搜索)
- /// <summary>
- /// 获取指定目录及子目录中所有文件列表
- /// </summary>
- /// <param name="directoryPath">指定目录的绝对路径</param>
- /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
- /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
- /// <param name="isSearchChild">是否搜索子目录</param>
- public static string[] GetFileNames(string directoryPath, string searchPattern, bool isSearchChild)
- {
- //如果目录不存在,则抛出异常
- if (!IsExistDirectory(directoryPath))
- {
- throw new FileNotFoundException();
- }
- try
- {
- if (isSearchChild)
- {
- return Directory.GetFiles(directoryPath, searchPattern, SearchOption.AllDirectories);
- }
- else
- {
- return Directory.GetFiles(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
- }
- }
- catch (IOException ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 根据时间获取指定路径目录下的 指定后缀名的 的所有文件
- /// <summary>
- /// 根据时间获取指定路径目录下的 指定后缀名的 的所有文件
- /// </summary>
- /// <param name="path">文件路径</param>
- /// <param name="Extension">后缀名 比如.txt</param>
- /// <returns></returns>
- public static DataRow[] GetFilesByTime(string path, string Extension)
- {
- if (Directory.Exists(path))
- {
- string fielExts = string.Format("*{0}", Extension);
- string[] files = Directory.GetFiles(path, fielExts);
- if (files.Length > 0)
- {
- DataTable table = new DataTable();
- table.Columns.Add(new DataColumn("filename", Type.GetType("System.String")));
- table.Columns.Add(new DataColumn("createtime", Type.GetType("System.DateTime")));
- for (int i = 0; i < files.Length; i++)
- {
- DataRow row = table.NewRow();
- DateTime creationTime = System.IO.File.GetCreationTime(files[i]);
- string fileName = Path.GetFileName(files[i]);
- row["filename"] = fileName;
- row["createtime"] = creationTime;
- table.Rows.Add(row);
- }
- return table.Select(string.Empty, "createtime desc");
- }
- }
- return new DataRow[0];
- }
- #endregion
- #region 检测指定目录中是否存在指定的文件
- /// <summary>
- /// 检测指定目录中是否存在指定的文件
- /// </summary>
- /// <param name="directoryPath">指定目录的绝对路径</param>
- /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
- /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
- /// <param name="isSearchChild">是否搜索子目录</param>
- public static bool Contains(string directoryPath, string searchPattern, bool isSearchChild = false)
- {
- try
- {
- //获取指定的文件列表
- string[] fileNames = GetFileNames(directoryPath, searchPattern, isSearchChild);
- //判断指定文件是否存在
- if (fileNames.Length == 0)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- catch (Exception ex)
- {
- throw new Exception(ex.Message);
- //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
- }
- }
- #endregion
- #region 检测指定目录是否为空
- /// <summary>
- /// 检测指定目录是否为空
- /// </summary>
- /// <param name="directoryPath">指定目录的绝对路径</param>
- public static bool IsEmptyDirectory(string directoryPath)
- {
- try
- {
- //判断是否存在文件
- string[] fileNames = GetFileNames(directoryPath);
- if (fileNames.Length > 0)
- {
- return false;
- }
- //判断是否存在文件夹
- string[] directoryNames = GetDirectories(directoryPath);
- if (directoryNames.Length > 0)
- {
- return false;
- }
- return true;
- }
- catch
- {
- //这里记录日志
- //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
- return true;
- }
- }
- #endregion
- #region 创建目录
- /// <summary>
- /// 创建目录
- /// </summary>
- /// <param name="dir">要创建的目录路径包括目录名</param>
- public static void CreateDir(string dir)
- {
- if (dir.Length == 0) return;
- string rootPath = ConfigHelper.GetValue<string>("baseDir");
- if (!Directory.Exists(rootPath + "\\" + dir))
- Directory.CreateDirectory(rootPath + "\\" + dir);
- }
- #endregion
- #region 删除目录
- /// <summary>
- /// 删除目录
- /// </summary>
- /// <param name="dir">要删除的目录路径和名称</param>
- public static void DeleteDir(string dir)
- {
- if (dir.Length == 0) return;
- string rootPath = ConfigHelper.GetValue<string>("baseDir");
- if (Directory.Exists(rootPath + "\\" + dir))
- Directory.Delete(rootPath + "\\" + dir);
- }
- #endregion
- #region 清空指定目录
- /// <summary>
- /// 清空指定目录下所有文件及子目录,但该目录依然保存.
- /// </summary>
- /// <param name="directoryPath">指定目录的绝对路径</param>
- public static void ClearDirectory(string directoryPath)
- {
- directoryPath = ConfigHelper.GetValue<string>("baseDir")+ @"/" + directoryPath;
- if (IsExistDirectory(directoryPath))
- {
- //删除目录中所有的文件
- string[] fileNames = GetFileNames(directoryPath);
- for (int i = 0; i < fileNames.Length; i++)
- {
- DeleteFile(fileNames[i]);
- }
- //删除目录中所有的子目录
- string[] directoryNames = GetDirectories(directoryPath);
- for (int i = 0; i < directoryNames.Length; i++)
- {
- DeleteDir(directoryNames[i]);
- }
- }
- }
- #endregion
- #region 复制文件夹
- /// <summary>
- /// 复制文件夹(递归)
- /// </summary>
- /// <param name="varFromDirectory">源文件夹路径</param>
- /// <param name="varToDirectory">目标文件夹路径</param>
- public static void CopyFolder(string varFromDirectory, string varToDirectory)
- {
- Directory.CreateDirectory(varToDirectory);
- if (!Directory.Exists(varFromDirectory)) return;
- string[] directories = Directory.GetDirectories(varFromDirectory);
- if (directories.Length > 0)
- {
- foreach (string d in directories)
- {
- CopyFolder(d, varToDirectory + d.Substring(d.LastIndexOf("\\")));
- }
- }
- string[] files = Directory.GetFiles(varFromDirectory);
- if (files.Length > 0)
- {
- foreach (string s in files)
- {
- System.IO.File.Copy(s, varToDirectory + s.Substring(s.LastIndexOf("\\")), true);
- }
- }
- }
- #endregion
- #region 检测指定文件是否存在,如果存在返回true
- /// <summary>
- /// 检测指定文件是否存在,如果存在则返回true。
- /// </summary>
- /// <param name="filePath">文件的绝对路径</param>
- public static bool IsExistFile(string filePath)
- {
- return File.Exists(filePath);
- }
- /// <summary>
- /// 检测指定文件是否存在,如果存在则返回true。
- /// </summary>
- /// <param name="filePath">文件的相对路径</param>
- /// <returns></returns>
- public static bool IsExistFileEx(string filePath)
- {
- string rootPath = Assembly.GetExecutingAssembly().CodeBase.Replace("/bin/Learun.Util.DLL", "").Replace("file:///", "");
- return File.Exists(rootPath + filePath);
- }
- #endregion
- #region 创建文件
- /// <summary>
- /// 创建文件
- /// </summary>
- /// <param name="dir">相对路径</param>
- /// <param name="pagestr">文件内容</param>
- public static void CreateFile(string dir, string pagestr)
- {
- dir = dir.Replace("/", "\\");
- if (dir.IndexOf("\\") > -1)
- CreateDir(dir.Substring(0, dir.LastIndexOf("\\")));
- System.IO.StreamWriter sw = new System.IO.StreamWriter(ConfigHelper.GetValue<string>("baseDir") + "\\" + dir, false, System.Text.Encoding.GetEncoding("utf-8"));
- sw.Write(pagestr);
- sw.Close();
- }
- /// <summary>
- /// 创建一个文件。
- /// </summary>
- /// <param name="filePath">文件的绝对路径</param>
- public static void CreateFile(string filePath)
- {
- try
- {
- //如果文件不存在则创建该文件
- if (!IsExistFile(filePath))
- {
- //创建一个FileInfo对象
- FileInfo file = new FileInfo(filePath);
- //创建文件
- FileStream fs = file.Create();
- //关闭文件流
- fs.Close();
- }
- }
- catch (Exception ex)
- {
- //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
- throw ex;
- }
- }
- /// <summary>
- /// 创建一个文件,并将字节流写入文件。
- /// </summary>
- /// <param name="filePath">文件的绝对路径</param>
- /// <param name="buffer">二进制流数据</param>
- public static void CreateFile(string filePath, byte[] buffer)
- {
- try
- {
- //如果文件不存在则创建该文件
- if (!IsExistFile(filePath))
- {
- //创建一个FileInfo对象
- FileInfo file = new FileInfo(filePath);
- //创建文件
- FileStream fs = file.Create();
- //写入二进制流
- fs.Write(buffer, 0, buffer.Length);
- //关闭文件流
- fs.Close();
- }
- }
- catch (Exception ex)
- {
- //LogHelper.WriteTraceLog(TraceLogLevel.Error, ex.Message);
- throw ex;
- }
- }
- /// <summary>
- /// 创建文件
- /// </summary>
- /// <param name="path">绝对路径</param>
- /// <param name="content">内容</param>
- public static void CreateFileContent(string path, string content)
- {
- FileInfo fi = new FileInfo(path);
- var di = fi.Directory;
- if (!di.Exists)
- {
- di.Create();
- }
- StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.GetEncoding("utf-8"));
- sw.Write(content);
- sw.Close();
- }
- /// <summary>
- /// 创建文件
- /// </summary>
- /// <param name="path">绝对路径</param>
- /// <param name="content">内容</param>
- /// <param name="encoding">编码格式</param>
- public static void CreateFileContent(string path, string content, string encoding)
- {
- FileInfo fi = new FileInfo(path);
- var di = fi.Directory;
- if (!di.Exists)
- {
- di.Create();
- }
- StreamWriter sw = new StreamWriter(path, false, System.Text.Encoding.GetEncoding(encoding));
- sw.Write(content);
- sw.Close();
- }
- #endregion
- #region 删除文件
- /// <summary>
- /// 删除文件
- /// </summary>
- /// <param name="file">要删除的文件路径和名称</param>
- public static void DeleteFile(string file)
- {
- if (System.IO.File.Exists(ConfigHelper.GetValue<string>("baseDir")+@"\" + file))
- {
- System.IO.File.Delete(ConfigHelper.GetValue<string>("baseDir") + @"\" + file);
- }
- }
- #endregion
- #region 移动文件(剪贴--粘贴)
- /// <summary>
- /// 移动文件(剪贴--粘贴)
- /// </summary>
- /// <param name="dir1">要移动的文件的路径及全名(包括后缀)</param>
- /// <param name="dir2">文件移动到新的位置,并指定新的文件名</param>
- public static void MoveFile(string dir1, string dir2)
- {
- dir1 = dir1.Replace("/", "\\");
- dir2 = dir2.Replace("/", "\\");
- if (System.IO.File.Exists(ConfigHelper.GetValue<string>("baseDir") + "\\" + dir1))
- System.IO.File.Move(ConfigHelper.GetValue<string>("baseDir") + "\\" + dir1, ConfigHelper.GetValue<string>("baseDir") + "\\" + dir2);
- }
- #endregion
- #region 复制文件
- /// <summary>
- /// 复制文件
- /// </summary>
- /// <param name="dir1">要复制的文件的路径已经全名(包括后缀)</param>
- /// <param name="dir2">目标位置,并指定新的文件名</param>
- public static void CopyFile(string dir1, string dir2)
- {
- dir1 = dir1.Replace("/", "\\");
- dir2 = dir2.Replace("/", "\\");
- if (System.IO.File.Exists(ConfigHelper.GetValue<string>("baseDir") + "\\" + dir1))
- {
- System.IO.File.Copy(ConfigHelper.GetValue<string>("baseDir") + "\\" + dir1, ConfigHelper.GetValue<string>("baseDir") + "\\" + dir2, true);
- }
- }
- #endregion
- #region 检查文件,如果文件不存在则创建
- /// <summary>
- /// 检查文件,如果文件不存在则创建
- /// </summary>
- /// <param name="FilePath">路径,包括文件名</param>
- public static void ExistsFile(string FilePath)
- {
- //if(!File.Exists(FilePath))
- //File.Create(FilePath);
- //以上写法会报错,详细解释请看下文.........
- if (!System.IO.File.Exists(FilePath))
- {
- FileStream fs = System.IO.File.Create(FilePath);
- fs.Close();
- }
- }
- #endregion
- #region 删除指定文件夹对应其他文件夹里的文件
- /// <summary>
- /// 删除指定文件夹对应其他文件夹里的文件
- /// </summary>
- /// <param name="varFromDirectory">指定文件夹路径</param>
- /// <param name="varToDirectory">对应其他文件夹路径</param>
- public static void DeleteFolderFiles(string varFromDirectory, string varToDirectory)
- {
- Directory.CreateDirectory(varToDirectory);
- if (!Directory.Exists(varFromDirectory)) return;
- string[] directories = Directory.GetDirectories(varFromDirectory);
- if (directories.Length > 0)
- {
- foreach (string d in directories)
- {
- DeleteFolderFiles(d, varToDirectory + d.Substring(d.LastIndexOf("\\")));
- }
- }
- string[] files = Directory.GetFiles(varFromDirectory);
- if (files.Length > 0)
- {
- foreach (string s in files)
- {
- System.IO.File.Delete(varToDirectory + s.Substring(s.LastIndexOf("\\")));
- }
- }
- }
- #endregion
- #region 从文件的绝对路径中获取文件名( 包含扩展名 )
- /// <summary>
- /// 从文件的绝对路径中获取文件名( 包含扩展名 )
- /// </summary>
- /// <param name="filePath">文件的绝对路径</param>
- public static string GetFileName(string filePath)
- {
- //获取文件的名称
- FileInfo fi = new FileInfo(filePath);
- return fi.Name;
- }
- #endregion
- #region 复制文件参考方法,页面中引用
- /// <summary>
- /// 复制文件参考方法,页面中引用
- /// </summary>
- /// <param name="cDir">新路径</param>
- /// <param name="TempId">模板引擎替换编号</param>
- public static void CopyFiles(string cDir, string TempId)
- {
- //if (Directory.Exists(Request.PhysicalApplicationPath + "\\Controls"))
- //{
- // string TempStr = string.Empty;
- // StreamWriter sw;
- // if (File.Exists(Request.PhysicalApplicationPath + "\\Controls\\Default.aspx"))
- // {
- // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\\Controls\\Default.aspx");
- // TempStr = TempStr.Replace("{$ChannelId$}", TempId);
- // sw = new StreamWriter(Request.PhysicalApplicationPath + "\\" + cDir + "\\Default.aspx", false, System.Text.Encoding.GetEncoding("GB2312"));
- // sw.Write(TempStr);
- // sw.Close();
- // }
- // if (File.Exists(Request.PhysicalApplicationPath + "\\Controls\\Column.aspx"))
- // {
- // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\\Controls\\Column.aspx");
- // TempStr = TempStr.Replace("{$ChannelId$}", TempId);
- // sw = new StreamWriter(Request.PhysicalApplicationPath + "\\" + cDir + "\\List.aspx", false, System.Text.Encoding.GetEncoding("GB2312"));
- // sw.Write(TempStr);
- // sw.Close();
- // }
- // if (File.Exists(Request.PhysicalApplicationPath + "\\Controls\\Content.aspx"))
- // {
- // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\\Controls\\Content.aspx");
- // TempStr = TempStr.Replace("{$ChannelId$}", TempId);
- // sw = new StreamWriter(Request.PhysicalApplicationPath + "\\" + cDir + "\\View.aspx", false, System.Text.Encoding.GetEncoding("GB2312"));
- // sw.Write(TempStr);
- // sw.Close();
- // }
- // if (File.Exists(Request.PhysicalApplicationPath + "\\Controls\\MoreDiss.aspx"))
- // {
- // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\\Controls\\MoreDiss.aspx");
- // TempStr = TempStr.Replace("{$ChannelId$}", TempId);
- // sw = new StreamWriter(Request.PhysicalApplicationPath + "\\" + cDir + "\\DissList.aspx", false, System.Text.Encoding.GetEncoding("GB2312"));
- // sw.Write(TempStr);
- // sw.Close();
- // }
- // if (File.Exists(Request.PhysicalApplicationPath + "\\Controls\\ShowDiss.aspx"))
- // {
- // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\\Controls\\ShowDiss.aspx");
- // TempStr = TempStr.Replace("{$ChannelId$}", TempId);
- // sw = new StreamWriter(Request.PhysicalApplicationPath + "\\" + cDir + "\\Diss.aspx", false, System.Text.Encoding.GetEncoding("GB2312"));
- // sw.Write(TempStr);
- // sw.Close();
- // }
- // if (File.Exists(Request.PhysicalApplicationPath + "\\Controls\\Review.aspx"))
- // {
- // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\\Controls\\Review.aspx");
- // TempStr = TempStr.Replace("{$ChannelId$}", TempId);
- // sw = new StreamWriter(Request.PhysicalApplicationPath + "\\" + cDir + "\\Review.aspx", false, System.Text.Encoding.GetEncoding("GB2312"));
- // sw.Write(TempStr);
- // sw.Close();
- // }
- // if (File.Exists(Request.PhysicalApplicationPath + "\\Controls\\Search.aspx"))
- // {
- // TempStr = File.ReadAllText(Request.PhysicalApplicationPath + "\\Controls\\Search.aspx");
- // TempStr = TempStr.Replace("{$ChannelId$}", TempId);
- // sw = new StreamWriter(Request.PhysicalApplicationPath + "\\" + cDir + "\\Search.aspx", false, System.Text.Encoding.GetEncoding("GB2312"));
- // sw.Write(TempStr);
- // sw.Close();
- // }
- //}
- }
- #endregion
- #region 获取文本文件的行数
- /// <summary>
- /// 获取文本文件的行数
- /// </summary>
- /// <param name="filePath">文件的绝对路径</param>
- public static int GetLineCount(string filePath)
- {
- //将文本文件的各行读到一个字符串数组中
- string[] rows = System.IO.File.ReadAllLines(filePath);
- //返回行数
- return rows.Length;
- }
- #endregion
- #region 获取一个文件的长度
- /// <summary>
- /// 获取一个文件的长度,单位为Byte
- /// </summary>
- /// <param name="filePath">文件的绝对路径</param>
- public static int GetFileSize(string filePath)
- {
- //创建一个文件对象
- FileInfo fi = new FileInfo(filePath);
- //获取文件的大小
- return (int)fi.Length;
- }
- #endregion
- #region 获取指定目录中的子目录列表
- /// <summary>
- /// 获取指定目录及子目录中所有子目录列表
- /// </summary>
- /// <param name="directoryPath">指定目录的绝对路径</param>
- /// <param name="searchPattern">模式字符串,"*"代表0或N个字符,"?"代表1个字符。
- /// 范例:"Log*.xml"表示搜索所有以Log开头的Xml文件。</param>
- /// <param name="isSearchChild">是否搜索子目录</param>
- public static string[] GetDirectories(string directoryPath, string searchPattern, bool isSearchChild)
- {
- try
- {
- if (isSearchChild)
- {
- return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.AllDirectories);
- }
- else
- {
- return Directory.GetDirectories(directoryPath, searchPattern, SearchOption.TopDirectoryOnly);
- }
- }
- catch (IOException ex)
- {
- throw ex;
- }
- }
- #endregion
- #region 向文本文件写入内容
- /// <summary>
- /// 向文本文件中写入内容
- /// </summary>
- /// <param name="filePath">文件的绝对路径</param>
- /// <param name="text">写入的内容</param>
- /// <param name="encoding">编码</param>
- public static void WriteText(string filePath, string text, Encoding encoding)
- {
- //向文件写入内容
- System.IO.File.WriteAllText(filePath, text, encoding);
- }
- #endregion
- #region 向文本文件的尾部追加内容
- /// <summary>
- /// 向文本文件的尾部追加内容
- /// </summary>
- /// <param name="filePath">文件的绝对路径</param>
- /// <param name="content">写入的内容</param>
- public static void AppendText(string filePath, string content)
- {
- System.IO.File.AppendAllText(filePath, content);
- }
- #endregion
- #region 将现有文件的内容复制到新文件中
- /// <summary>
- /// 将源文件的内容复制到目标文件中
- /// </summary>
- /// <param name="sourceFilePath">源文件的绝对路径</param>
- /// <param name="destFilePath">目标文件的绝对路径</param>
- public static void Copy(string sourceFilePath, string destFilePath)
- {
- System.IO.File.Copy(sourceFilePath, destFilePath, true);
- }
- #endregion
- #region 将文件移动到指定目录
- /// <summary>
- /// 将文件移动到指定目录
- /// </summary>
- /// <param name="sourceFilePath">需要移动的源文件的绝对路径</param>
- /// <param name="descDirectoryPath">移动到的目录的绝对路径</param>
- public static void Move(string sourceFilePath, string descDirectoryPath)
- {
- //获取源文件的名称
- string sourceFileName = GetFileName(sourceFilePath);
- if (IsExistDirectory(descDirectoryPath))
- {
- //如果目标中存在同名文件,则删除
- if (IsExistFile(descDirectoryPath + "\\" + sourceFileName))
- {
- DeleteFile(descDirectoryPath + "\\" + sourceFileName);
- }
- //将文件移动到指定目录
- System.IO.File.Move(sourceFilePath, descDirectoryPath + "\\" + sourceFileName);
- }
- }
- #endregion
- #region 从文件的绝对路径中获取文件名( 不包含扩展名 )
- /// <summary>
- /// 从文件的绝对路径中获取文件名( 不包含扩展名 )
- /// </summary>
- /// <param name="filePath">文件的绝对路径</param>
- public static string GetFileNameNoExtension(string filePath)
- {
- //获取文件的名称
- FileInfo fi = new FileInfo(filePath);
- return fi.Name.Split('.')[0];
- }
- #endregion
- #region 从文件的绝对路径中获取扩展名
- /// <summary>
- /// 从文件的绝对路径中获取扩展名
- /// </summary>
- /// <param name="filePath">文件的绝对路径</param>
- public static string GetExtension(string filePath)
- {
- //获取文件的名称
- FileInfo fi = new FileInfo(filePath);
- return fi.Extension;
- }
- #endregion
- #region 清空文件内容
- /// <summary>
- /// 清空文件内容
- /// </summary>
- /// <param name="filePath">文件的绝对路径</param>
- public static void ClearFile(string filePath)
- {
- //删除文件
- System.IO.File.Delete(filePath);
- //重新创建该文件
- CreateFile(filePath);
- }
- #endregion
- #region 获取文件大小并以B,KB,GB,TB
- /// <summary>
- /// 计算文件大小函数(保留两位小数),Size为字节大小
- /// </summary>
- /// <param name="size">初始文件大小</param>
- /// <returns></returns>
- public static string ToFileSize(long size)
- {
- string m_strSize = "";
- long FactSize = 0;
- FactSize = size;
- if (FactSize < 1024.00)
- m_strSize = FactSize.ToString("F2") + " 字节";
- else if (FactSize >= 1024.00 && FactSize < 1048576)
- m_strSize = (FactSize / 1024.00).ToString("F2") + " KB";
- else if (FactSize >= 1048576 && FactSize < 1073741824)
- m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " MB";
- else if (FactSize >= 1073741824)
- m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " GB";
- return m_strSize;
- }
- #endregion
- #region 返回绝对路径
- /// <summary>
- /// 返回绝对路径
- /// </summary>
- /// <param name="filePath">相对路径</param>
- /// <returns></returns>
- public static string GetAbsolutePath(string filePath)
- {
- return ConfigHelper.GetValue<string>("baseDir") + @"\" + filePath;
- }
- #endregion
- }
- }
|