详细介绍参考:http://blog.csdn.net/wangyue4/article/details/4616801

源码举例:

  1. public class FileSystemManager
  2. {
  3. private static string strRootFolder;
  4.  
  5. static FileSystemManager()
  6. {
  7. strRootFolder = HttpContext.Current.Request.PhysicalApplicationPath;
  8. strRootFolder = strRootFolder.Substring(0, strRootFolder.LastIndexOf(@"\"));
  9.  
  10. }
  11.  
  12. /// <summary>
  13. /// 读根目录
  14. /// </summary>
  15. /// <returns></returns>
  16. public static string GetRootPath()
  17. {
  18. return strRootFolder;
  19. }
  20.  
  21. /// <summary>
  22. /// 写根目录
  23. /// </summary>
  24. /// <param name="path"></param>
  25. public static void SetRootPath(string path)
  26. {
  27. strRootFolder = path;
  28. }
  29.  
  30. /// <summary>
  31. /// 读取列表
  32. /// </summary>
  33. /// <returns></returns>
  34. public static List<FileSystemItem> GetItems()
  35. {
  36. return GetItems(strRootFolder);
  37. }
  38.  
  39. /// <summary>
  40. /// 读取列表
  41. /// </summary>
  42. /// <param name="path"></param>
  43. /// <returns></returns>
  44. public static List<FileSystemItem> GetItems(string path)
  45. {
  46. string[] folders = Directory.GetDirectories(path);
  47. string[] files = Directory.GetFiles(path);
  48. List<FileSystemItem> list = new List<FileSystemItem>();
  49. foreach (string s in folders)
  50. {
  51. FileSystemItem item = new FileSystemItem();
  52. DirectoryInfo di = new DirectoryInfo(s);
  53. item.Name = di.Name;
  54. item.FullName = di.FullName;
  55. item.CreationDate = di.CreationTime;
  56. item.IsFolder = true;
  57. list.Add(item);
  58. }
  59. foreach (string s in files)
  60. {
  61. FileSystemItem item = new FileSystemItem();
  62. FileInfo fi = new FileInfo(s);
  63. item.Name = fi.Name;
  64. item.FullName = fi.FullName;
  65. item.CreationDate = fi.CreationTime;
  66. item.IsFolder = false;
  67. item.Size = fi.Length;
  68. list.Add(item);
  69. }
  70.  
  71. return list;
  72. }
  73.  
  74. /// <summary>
  75. /// 读取文件夹
  76. /// </summary>
  77. /// <param name="name"></param>
  78. /// <param name="parentName"></param>
  79. public static void CreateFolder(string name, string parentName)
  80. {
  81. DirectoryInfo di = new DirectoryInfo(parentName);
  82. di.CreateSubdirectory(name);
  83. }
  84.  
  85. /// <summary>
  86. /// 删除文件夹
  87. /// </summary>
  88. /// <param name="path"></param>
  89. public static void DeleteFolder(string path)
  90. {
  91. Directory.Delete(path);
  92. }
  93.  
  94. /// <summary>
  95. /// 移动文件夹
  96. /// </summary>
  97. /// <param name="oldPath"></param>
  98. /// <param name="newPath"></param>
  99. public static void MoveFolder(string oldPath, string newPath)
  100. {
  101. Directory.Move(oldPath, newPath);
  102. }
  103.  
  104. /// <summary>
  105. /// 创建文件
  106. /// </summary>
  107. /// <param name="filename"></param>
  108. /// <param name="path"></param>
  109. public static void CreateFile(string filename, string path)
  110. {
  111. FileStream fs = File.Create(path + "\\" + filename);
  112. fs.Close();
  113. }
  114.  
  115. /// <summary>
  116. /// 创建文件
  117. /// </summary>
  118. /// <param name="filename"></param>
  119. /// <param name="path"></param>
  120. /// <param name="contents"></param>
  121. public static void CreateFile(string filename, string path, byte[] contents)
  122. {
  123. FileStream fs = File.Create(path + "\\" + filename);
  124. fs.Write(contents, 0, contents.Length);
  125. fs.Close();
  126. }
  127.  
  128. /// <summary>
  129. /// 删除文件
  130. /// </summary>
  131. /// <param name="path"></param>
  132. public static void DeleteFile(string path)
  133. {
  134. File.Delete(path);
  135. }
  136.  
  137. /// <summary>
  138. /// 移动文件
  139. /// </summary>
  140. /// <param name="oldPath"></param>
  141. /// <param name="newPath"></param>
  142. public static void MoveFile(string oldPath, string newPath)
  143. {
  144. File.Move(oldPath, newPath);
  145. }
  146. /// <summary>
  147. /// 复制文件
  148. /// </summary>
  149. /// <param name="oldPath"></param>
  150. /// <param name="newPath"></param>
  151. public static void CopyFile(string oldPath, string newPath)
  152. {
  153. File.Copy(oldPath, newPath, true);
  154. }
  155. /// <summary>
  156. /// 读取文件信息
  157. /// </summary>
  158. /// <param name="path"></param>
  159. /// <returns></returns>
  160. public static FileSystemItem GetItemInfo(string path)
  161. {
  162. FileSystemItem item = new FileSystemItem();
  163. if (Directory.Exists(path))
  164. {
  165. DirectoryInfo di = new DirectoryInfo(path);
  166. item.Name = di.Name;
  167. item.FullName = di.FullName;
  168. item.CreationDate = di.CreationTime;
  169. item.IsFolder = true;
  170. item.LastAccessDate = di.LastAccessTime;
  171. item.LastWriteDate = di.LastWriteTime;
  172. item.FileCount = di.GetFiles().Length;
  173. item.SubFolderCount = di.GetDirectories().Length;
  174. }
  175. else
  176. {
  177. FileInfo fi = new FileInfo(path);
  178. item.Name = fi.Name;
  179. item.FullName = fi.FullName;
  180. item.CreationDate = fi.CreationTime;
  181. item.LastAccessDate = fi.LastAccessTime;
  182. item.LastWriteDate = fi.LastWriteTime;
  183. item.IsFolder = false;
  184. item.Size = fi.Length;
  185. }
  186. return item;
  187. }
  188.  
  189. /// <summary>
  190. /// 复制文件夹
  191. /// </summary>
  192. /// <param name="source"></param>
  193. /// <param name="destination"></param>
  194. public static void CopyFolder(string source, string destination)
  195. {
  196. String[] files;
  197. if (destination[destination.Length - 1] != Path.DirectorySeparatorChar)
  198. destination += Path.DirectorySeparatorChar;
  199. if (!Directory.Exists(destination)) Directory.CreateDirectory(destination);
  200. files = Directory.GetFileSystemEntries(source);
  201. foreach (string element in files)
  202. {
  203. if (Directory.Exists(element))
  204. CopyFolder(element, destination + Path.GetFileName(element));
  205. else
  206. File.Copy(element, destination + Path.GetFileName(element), true);
  207. }
  208. }

  

  1. public class FileSystemItem
  2. {
  3. private string _Name;
  4. private string _FullName;
  5.  
  6. private DateTime _CreationDate;
  7. private DateTime _LastAccessDate;
  8. private DateTime _LastWriteDate;
  9.  
  10. private bool _IsFolder;
  11.  
  12. private long _Size;
  13. private long _FileCount;
  14. private long _SubFolderCount;
  15.  
  16. private string _Version;
  17.  
  18. /// <summary>
  19. /// 名称
  20. /// </summary>
  21. public string Name
  22. {
  23. get
  24. {
  25. return _Name;
  26. }
  27. set
  28. {
  29. _Name = value;
  30. }
  31. }
  32.  
  33. /// <summary>
  34. /// 完整目录
  35. /// </summary>
  36. public string FullName
  37. {
  38. get
  39. {
  40. return _FullName;
  41. }
  42. set
  43. {
  44. _FullName = value;
  45. }
  46. }
  47.  
  48. /// <summary>
  49. /// 创建时间
  50. /// </summary>
  51. public DateTime CreationDate
  52. {
  53. get
  54. {
  55. return _CreationDate;
  56. }
  57. set
  58. {
  59. _CreationDate = value;
  60. }
  61. }
  62.  
  63. /// <summary>
  64. /// 是否是文件夹
  65. /// </summary>
  66. public bool IsFolder
  67. {
  68. get
  69. {
  70. return _IsFolder;
  71. }
  72. set
  73. {
  74. _IsFolder = value;
  75. }
  76. }
  77.  
  78. /// <summary>
  79. /// 大小
  80. /// </summary>
  81. public long Size
  82. {
  83. get
  84. {
  85. return _Size;
  86. }
  87. set
  88. {
  89. _Size = value;
  90. }
  91. }
  92.  
  93. /// <summary>
  94. /// 访问时间
  95. /// </summary>
  96. public DateTime LastAccessDate
  97. {
  98. get
  99. {
  100. return _LastAccessDate;
  101. }
  102. set
  103. {
  104. _LastAccessDate = value;
  105. }
  106. }
  107.  
  108. /// <summary>
  109. /// 修改时间
  110. /// </summary>
  111. public DateTime LastWriteDate
  112. {
  113. get
  114. {
  115. return _LastWriteDate;
  116. }
  117. set
  118. {
  119. _LastWriteDate = value;
  120. }
  121. }
  122.  
  123. /// <summary>
  124. /// 文件数
  125. /// </summary>
  126. public long FileCount
  127. {
  128. get
  129. {
  130. return _FileCount;
  131. }
  132. set
  133. {
  134. _FileCount = value;
  135. }
  136. }
  137.  
  138. /// <summary>
  139. /// 文件夹数
  140. /// </summary>
  141. public long SubFolderCount
  142. {
  143. get
  144. {
  145. return _SubFolderCount;
  146. }
  147. set
  148. {
  149. _SubFolderCount = value;
  150. }
  151. }
  152.  
  153. /// <summary>
  154. /// 版本
  155. /// </summary>
  156. /// <returns></returns>
  157. public string Version()
  158. {
  159. if (_Version == null)
  160. _Version = GetType().Assembly.GetName().Version.ToString();
  161.  
  162. return _Version;
  163. }
  164.  
  165. }

  

C#中对文件的操作的更多相关文章

  1. linux下拷贝命令中的文件过滤操作记录

    在日常的运维工作中,经常会涉及到在拷贝某个目录时要排查其中的某些文件.废话不多说,下面对这一需求的操作做一记录: linux系统中,假设要想将目录A中的文件复制到目录B中,并且复制时过滤掉源目录A中的 ...

  2. [转]Windows系统中监控文件复制操作的几种方式

    1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得到源和目标文件名. 可以控制拒绝操作. 缺点: 不能对文件进行控制. 只对Shell文件操作有效, 对原生 ...

  3. UWP中的文件相关操作

    最近开始做UWP开发,图省事儿就把自己之前一个Winform项目的一部分代码拷贝到了新写的UWP项目中来.整出了一些幺蛾子,下面做一个记录. 首先提一个重点就是:UWP里关于文件的操作尽量用Stora ...

  4. grep sed awk 3个Linux中对文件内容操作的命令

    在学习Linux命令中,发现3个有关于文件内容操作的命令grep,sed和awk,在这里简单汇总这3个命令主要作用,在实际中找到最合适的情景应用,详细用法可以参考其他文章. 1.grep命令 主要作用 ...

  5. Windows系统中监控文件复制操作的几种方式

    http://blog.sina.com.cn/s/blog_4596beaa0100lp4y.html 1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得 ...

  6. Android中的文件权限操作

    默认本工程创建的文件本工程对其有读写权限. 我们可以通过context.openFileOutput("文件名", 模式): 我们可以创建私有, 共有, 只读, 只写文件, 默认的 ...

  7. linux中普通的文件查看操作(cat、more、less、head、tail)

    cat:基本是最常用的查看文件内容的linux命令. more 也是用来查看一个文件的内容.当文件内容太多,一屏幕不能占下,而你用cat肯定是看不前面的内容的,那么使用more就可以解决这个问题了.当 ...

  8. Ubuntu中拷贝文件的操作

    cp(copy)命令 该命令的功能是将给出的文件或目录拷贝到另一文件或目录中. 语法: cp [选项] 源文件或目录 目标文件或目录 说明:该命令把指定的源文件复制到目标文件或把多个源文件复制到目标目 ...

  9. Python中的文件IO操作(读写文件、追加文件)

    Python中文件的读写包含三个步骤:打开文件,读/写文件,关闭文件. 文件打开之后必须关闭,因为在磁盘上读写文件的功能是由操作系统提供的,文件作为对象,被打开后会占用操作系统的资源,而操作系统在同一 ...

  10. .NET中的文件IO操作实例

    1.写入文件代码: //1.1 生成文件名和设置文件物理路径 Random random = new Random(DateTime.Now.Millisecond); ); string Physi ...

随机推荐

  1. Single Number i and ii

    Single Number Given an array of integers, every element appears twice except for one. Find that sing ...

  2. 我的Python成长之路---第八天---Python基础(23)---2016年3月5日(晴)

    socketserver 之前讲道德socket模块是单进程的,只能接受一个客户端的连接和请求,只有当该客户端断开的之后才能再接受来自其他客户端的连接和请求.当然我们也可以通过python的多线程等模 ...

  3. android自动化(appium)

    目录 一.Appium环境搭建 1.下载nodejs,并安装 2.下载appium,并安装 3.安装python.安装pip.安装appium 4.安装java的jdk 5.安装andriod的sdk ...

  4. 顺序队列之C++实现

    下面介绍下用C++实现的顺序队列,在VC6下调试通过. 1.文件组织形式 2.sq.h顺序队列类的说明 #ifndef _SQ_H_ #define _SQ_H_ typedef int dataTy ...

  5. Java学习之自定义异常

    1 package com.gh; import java.util.Scanner; /** * 自定义异常 * @author ganhang * */ public class Exceptio ...

  6. C++动态数组的实现

    #include <iostream> using namespace std; int main() { int n; while(cin>>n) { ]; p[]=; p[ ...

  7. OpenStack里对VPN的支持

    今天翻自己的笔记找到了点去年研究Cloudpipe的东西: 对于用VLAN隔开的项目内主机的访问,可以使用CloudPipe来进行VPN访问 其实就是把OpenStack和OpenVPN集成了一下,给 ...

  8. vim下使用YouCompleteMe实现代码提示、补全以及跳转设置

    配置YouCompleteMe 1. 安装vundle vundle是一个管理vim插件的工具,使用vundle安装YouCompleteMe比较方便. 按照作者在https://github.com ...

  9. windows 不能在 本地计算机 启动 Apache

    可能是Apache 的监听端口与其他软件有冲突,这是新手常犯的一个错误,Windows安装了IIS服务器的同时,又安装Apache服务器,二个服务器软件都监听TCP/IP协议的80端口,于是就有其中的 ...

  10. HDU 1969(二分法)

    My birthday is coming up and traditionally I’m serving pie. Not just one pie, no, I have a number N ...