根据查阅的资料对代码进行修改并完善备注后的结果。希望能对新手有所帮助。 1 using System; 6 using System.IO; 8 namespace 文件操作类 9 { 10 public class FileHelper 11 { 12 /// <summary> 13 /// 判断文件是否存在 14 /// </summary> 15 /// <param name="filePath">文件全路径</param> 16 /// <returns></returns> 17 public static bool Exists(string filePath) 18 { 19 if (filePath == null || filePath.Trim() == "") 20 { 21 return false; 22 } 23 24 if (File.Exists(filePath)) 25 { 26 return true; 27 } 28 29 return false; 30 } 31 32 33 /// <summary> 34 /// 创建文件夹 35 /// </summary> 36 /// <param name="dirPath">文件夹路径</param> 37 /// <returns></returns> 38 public static bool CreateDir(string dirPath) 39 { 40 if (!Directory.Exists(dirPath)) 41 { 42 Directory.CreateDirectory(dirPath); 43 } 44 return true; 45 } 46 47 48 /// <summary> 49 /// 创建文件 50 /// </summary> 51 /// <param name="filePath">文件路径</param> 52 /// <returns></returns> 53 public static bool CreateFile(string filePath) 54 { 55 if (!File.Exists(filePath)) 56 { 57 FileStream fs = File.Create(filePath); 58 fs.Close(); 59 fs.Dispose(); 60 } 61 return true; 62 63 } 64 65 66 /// <summary> 67 /// 读文件内容 68 /// </summary> 69 /// <param name="filePath">文件路径</param> 70 /// <param name="encoding">编码格式</param> 71 /// <returns></returns> 72 public static string Read(string filePath,Encoding encoding) 73 { 74 if (!Exists(filePath)) 75 { 76 return null; 77 } 78 //将文件信息读入流中 79 using (FileStream fs = new FileStream(filePath,FileMode.Open)) 80 { 81 return new StreamReader(fs, encoding).ReadToEnd(); 82 } 83 } 84 85 /// <summary> 86 /// 读取文件的一行内容 87 /// </summary> 88 /// <param name="filePath">文件路径</param> 89 /// <param name="encoding">编码格式</param> 90 /// <returns></returns> 91 public static string ReadLine(string filePath, Encoding encoding) 92 { 93 if (!Exists(filePath)) 94 { 95 return null; 96 } 97 using (FileStream fs = new FileStream(filePath, FileMode.Open)) 98 { 99 return new StreamReader(fs, encoding).ReadLine(); 100 } 101 } 102 103 104 /// <summary> 105 /// 写文件 106 /// </summary> 107 /// <param name="filePath">文件路径</param> 108 /// <param name="content">文件内容</param> 109 /// <returns></returns> 110 public static bool Write(string filePath, string content) 111 { 112 if (!Exists(filePath) || content == null) 113 { 114 return false; 115 } 116 117 //将文件信息读入流中 118 using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) 119 { 120 lock (fs)//锁住流 121 { 122 if (!fs.CanWrite) 123 { 124 throw new System.Security.SecurityException("文件filePath=" + filePath + "是只读文件不能写入!"); 125 } 126 127 byte[] buffer = Encoding.Default.GetBytes(content); 128 fs.Write(buffer, 0, buffer.Length); 129 return true; 130 } 131 } 132 } 133 134 135 /// <summary> 136 /// 写入一行 137 /// </summary> 138 /// <param name="filePath">文件路径</param> 139 /// <param name="content">内容</param> 140 /// <returns></returns> 141 public static bool WriteLine(string filePath, string content) 142 { 143 using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate | FileMode.Append)) 144 { 145 lock (fs) 146 { 147 if (!fs.CanWrite) 148 { 149 throw new System.Security.SecurityException("文件filePath=" + filePath + "是只读文件不能写入!"); 150 } 151 152 StreamWriter sw = new StreamWriter(fs); 153 sw.WriteLine(content); 154 sw.Dispose(); 155 sw.Close(); 156 return true; 157 } 158 } 159 } 160 161 162 public static bool CopyDir(DirectoryInfo fromDir, string toDir) 163 { 164 return CopyDir(fromDir, toDir, fromDir.FullName); 165 } 166 167 168 /// <summary> 169 /// 复制目录 170 /// </summary> 171 /// <param name="fromDir">被复制的目录路径</param> 172 /// <param name="toDir">复制到的目录路径</param> 173 /// <returns></returns> 174 public static bool CopyDir(string fromDir, string toDir) 175 { 176 if (fromDir == null || toDir == null) 177 { 178 throw new NullReferenceException("参数为空"); 179 } 180 181 if (fromDir == toDir) 182 { 183 throw new Exception("两个目录都是" + fromDir); 184 } 185 186 if (!Directory.Exists(fromDir)) 187 { 188 throw new IOException("目录fromDir=" + fromDir + "不存在"); 189 } 190 191 DirectoryInfo dir = new DirectoryInfo(fromDir); 192 return CopyDir(dir, toDir, dir.FullName); 193 } 194 195 196 /// <summary> 197 /// 复制目录 198 /// </summary> 199 /// <param name="fromDir">被复制的目录路径</param> 200 /// <param name="toDir">复制到的目录路径</param> 201 /// <param name="rootDir">被复制的根目录路径</param> 202 /// <returns></returns> 203 private static bool CopyDir(DirectoryInfo fromDir, string toDir, string rootDir) 204 { 205 string filePath = string.Empty; 206 foreach (FileInfo f in fromDir.GetFiles()) 207 { 208 filePath = toDir + f.FullName.Substring(rootDir.Length); 209 string newDir = filePath.Substring(0, filePath.LastIndexOf("\\")); 210 CreateDir(newDir); 211 File.Copy(f.FullName, filePath, true); 212 } 213 214 foreach (DirectoryInfo dir in fromDir.GetDirectories()) 215 { 216 CopyDir(dir, toDir, rootDir); 217 } 218 219 return true; 220 } 221 222 223 /// <summary> 224 /// 删除文件 225 /// </summary> 226 /// <param name="filePath">文件的完整路径</param> 227 /// <returns></returns> 228 public static bool DeleteFile(string filePath) 229 { 230 if (Exists(filePath)) 231 { 232 File.Delete(filePath); 233 return true; 234 } 235 return false; 236 } 237 238 239 public static void DeleteDir(DirectoryInfo dir) 240 { 241 if (dir == null) 242 { 243 throw new NullReferenceException("目录不存在"); 244 } 245 246 foreach (DirectoryInfo d in dir.GetDirectories()) 247 { 248 DeleteDir(d); 249 } 250 251 foreach (FileInfo f in dir.GetFiles()) 252 { 253 DeleteFile(f.FullName); 254 } 255 256 dir.Delete(); 257 258 } 259 260 261 /// <summary> 262 /// 删除目录 263 /// </summary> 264 /// <param name="dir">指定目录路径</param> 265 /// <param name="onlyDir">是否只删除目录</param> 266 /// <returns></returns> 267 public static bool DeleteDir(string dir, bool onlyDir) 268 { 269 if (dir == null || dir.Trim() == "") 270 { 271 throw new NullReferenceException("目录dir=" + dir + "不存在"); 272 } 273 274 if (!Directory.Exists(dir)) 275 { 276 return false; 277 } 278 279 DirectoryInfo dirInfo = new DirectoryInfo(dir); 280 if (dirInfo.GetFiles().Length == 0 && dirInfo.GetDirectories().Length == 0) 281 { 282 Directory.Delete(dir); 283 return true; 284 } 285 286 287 if (!onlyDir) 288 { 289 return false; 290 } 291 else 292 { 293 DeleteDir(dirInfo); 294 return true; 295 } 296 297 } 298 299 300 /// <summary> 301 /// 在指定的目录中查找文件 302 /// </summary> 303 /// <param name="dir">目录路径</param> 304 /// <param name="fileName">文件名</param> 305 /// <returns></returns> 306 public static bool FindFile(string dir, string fileName) 307 { 308 if (dir == null || dir.Trim() == "" || fileName == null || fileName.Trim() == "" || !Directory.Exists(dir)) 309 { 310 return false; 311 } 312 313 DirectoryInfo dirInfo = new DirectoryInfo(dir); 314 return FindFile(dirInfo, fileName); 315 316 } 317 318 319 public static bool FindFile(DirectoryInfo dir, string fileName) 320 { 321 foreach (DirectoryInfo d in dir.GetDirectories()) 322 { 323 if (File.Exists(d.FullName + "\\" + fileName)) 324 { 325 return true; 326 } 327 FindFile(d, fileName); 328 } 329 330 return false; 331 } 332 333 } 334 }
转载请标出本博地址:http://www.cnblogs.com/codeToUp/p/4793153.html
时间: 2024-10-19 09:00:39