C#中对文件的操作
详细介绍参考:http://blog.csdn.net/wangyue4/article/details/4616801
源码举例:
public class FileSystemManager { private static string strRootFolder; static FileSystemManager() { strRootFolder = HttpContext.Current.Request.PhysicalApplicationPath; strRootFolder = strRootFolder.Substring(0, strRootFolder.LastIndexOf(@"\")); } /// <summary> /// 读根目录 /// </summary> /// <returns></returns> public static string GetRootPath() { return strRootFolder; } /// <summary> /// 写根目录 /// </summary> /// <param name="path"></param> public static void SetRootPath(string path) { strRootFolder = path; } /// <summary> /// 读取列表 /// </summary> /// <returns></returns> public static List<FileSystemItem> GetItems() { return GetItems(strRootFolder); } /// <summary> /// 读取列表 /// </summary> /// <param name="path"></param> /// <returns></returns> public static List<FileSystemItem> GetItems(string path) { string[] folders = Directory.GetDirectories(path); string[] files = Directory.GetFiles(path); List<FileSystemItem> list = new List<FileSystemItem>(); foreach (string s in folders) { FileSystemItem item = new FileSystemItem(); DirectoryInfo di = new DirectoryInfo(s); item.Name = di.Name; item.FullName = di.FullName; item.CreationDate = di.CreationTime; item.IsFolder = true; list.Add(item); } foreach (string s in files) { FileSystemItem item = new FileSystemItem(); FileInfo fi = new FileInfo(s); item.Name = fi.Name; item.FullName = fi.FullName; item.CreationDate = fi.CreationTime; item.IsFolder = false; item.Size = fi.Length; list.Add(item); } return list; } /// <summary> /// 读取文件夹 /// </summary> /// <param name="name"></param> /// <param name="parentName"></param> public static void CreateFolder(string name, string parentName) { DirectoryInfo di = new DirectoryInfo(parentName); di.CreateSubdirectory(name); } /// <summary> /// 删除文件夹 /// </summary> /// <param name="path"></param> public static void DeleteFolder(string path) { Directory.Delete(path); } /// <summary> /// 移动文件夹 /// </summary> /// <param name="oldPath"></param> /// <param name="newPath"></param> public static void MoveFolder(string oldPath, string newPath) { Directory.Move(oldPath, newPath); } /// <summary> /// 创建文件 /// </summary> /// <param name="filename"></param> /// <param name="path"></param> public static void CreateFile(string filename, string path) { FileStream fs = File.Create(path + "\\" + filename); fs.Close(); } /// <summary> /// 创建文件 /// </summary> /// <param name="filename"></param> /// <param name="path"></param> /// <param name="contents"></param> public static void CreateFile(string filename, string path, byte[] contents) { FileStream fs = File.Create(path + "\\" + filename); fs.Write(contents, 0, contents.Length); fs.Close(); } /// <summary> /// 删除文件 /// </summary> /// <param name="path"></param> public static void DeleteFile(string path) { File.Delete(path); } /// <summary> /// 移动文件 /// </summary> /// <param name="oldPath"></param> /// <param name="newPath"></param> public static void MoveFile(string oldPath, string newPath) { File.Move(oldPath, newPath); } /// <summary> /// 复制文件 /// </summary> /// <param name="oldPath"></param> /// <param name="newPath"></param> public static void CopyFile(string oldPath, string newPath) { File.Copy(oldPath, newPath, true); } /// <summary> /// 读取文件信息 /// </summary> /// <param name="path"></param> /// <returns></returns> public static FileSystemItem GetItemInfo(string path) { FileSystemItem item = new FileSystemItem(); if (Directory.Exists(path)) { DirectoryInfo di = new DirectoryInfo(path); item.Name = di.Name; item.FullName = di.FullName; item.CreationDate = di.CreationTime; item.IsFolder = true; item.LastAccessDate = di.LastAccessTime; item.LastWriteDate = di.LastWriteTime; item.FileCount = di.GetFiles().Length; item.SubFolderCount = di.GetDirectories().Length; } else { FileInfo fi = new FileInfo(path); item.Name = fi.Name; item.FullName = fi.FullName; item.CreationDate = fi.CreationTime; item.LastAccessDate = fi.LastAccessTime; item.LastWriteDate = fi.LastWriteTime; item.IsFolder = false; item.Size = fi.Length; } return item; } /// <summary> /// 复制文件夹 /// </summary> /// <param name="source"></param> /// <param name="destination"></param> public static void CopyFolder(string source, string destination) { String[] files; if (destination[destination.Length - 1] != Path.DirectorySeparatorChar) destination += Path.DirectorySeparatorChar; if (!Directory.Exists(destination)) Directory.CreateDirectory(destination); files = Directory.GetFileSystemEntries(source); foreach (string element in files) { if (Directory.Exists(element)) CopyFolder(element, destination + Path.GetFileName(element)); else File.Copy(element, destination + Path.GetFileName(element), true); } } }
public class FileSystemItem { private string _Name; private string _FullName; private DateTime _CreationDate; private DateTime _LastAccessDate; private DateTime _LastWriteDate; private bool _IsFolder; private long _Size; private long _FileCount; private long _SubFolderCount; private string _Version; /// <summary> /// 名称 /// </summary> public string Name { get { return _Name; } set { _Name = value; } } /// <summary> /// 完整目录 /// </summary> public string FullName { get { return _FullName; } set { _FullName = value; } } /// <summary> /// 创建时间 /// </summary> public DateTime CreationDate { get { return _CreationDate; } set { _CreationDate = value; } } /// <summary> /// 是否是文件夹 /// </summary> public bool IsFolder { get { return _IsFolder; } set { _IsFolder = value; } } /// <summary> /// 大小 /// </summary> public long Size { get { return _Size; } set { _Size = value; } } /// <summary> /// 访问时间 /// </summary> public DateTime LastAccessDate { get { return _LastAccessDate; } set { _LastAccessDate = value; } } /// <summary> /// 修改时间 /// </summary> public DateTime LastWriteDate { get { return _LastWriteDate; } set { _LastWriteDate = value; } } /// <summary> /// 文件数 /// </summary> public long FileCount { get { return _FileCount; } set { _FileCount = value; } } /// <summary> /// 文件夹数 /// </summary> public long SubFolderCount { get { return _SubFolderCount; } set { _SubFolderCount = value; } } /// <summary> /// 版本 /// </summary> /// <returns></returns> public string Version() { if (_Version == null) _Version = GetType().Assembly.GetName().Version.ToString(); return _Version; } }