using System;
using System.IO;
namespace Utils
{
public class IOHelper
{
public IOHelper();
public static bool CopyDir(DirectoryInfo fromDir, string toDir); //復(fù)制目錄
public static bool CopyDir(string fromDir, string toDir); //復(fù)制目錄
public static bool CreateDir(string dirName); //創(chuàng)建目錄
public static bool CreateFile(string fileName); //創(chuàng)建文件
public static void DeleteDir(DirectoryInfo dir); //刪除目錄 (如果目錄中存在文件就刪除)
public static bool DeleteDir(string dir, bool onlyDir); //刪除目錄
public static bool DeleteFile(string fileName);//刪除文件
public static bool Exists(string fileName);//判斷文件是否存在
public static bool FindFile(DirectoryInfo dir, string fileName);//在指定的目錄中查找文件
public static bool FindFile(string dir, string fileName);//在指定的目錄中查找文件
public static string Read(string fileName);//讀文件的全部?jī)?nèi)容
public static string ReadLine(string fileName);//讀第一行數(shù)據(jù)
public static bool Write(string fileName, string content);//寫入指定的內(nèi)容
public static bool WriteLine(string fileName, string content);//寫一行數(shù)據(jù)
}
}
using System;
using System.Text;
using System.IO;
/*----------------------------------------------------------------
//文件名:IOHelper
//文件功能描述:文件操作類
//
//創(chuàng)建人:陳太漢
//創(chuàng)建日期:2011/05/18
----------------------------------------------------------------*/
namespace Utils
{
public class IOHelper
{
/// <summary>
/// 判斷文件是否存在
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static bool Exists(string fileName)
{
if (fileName == null || fileName.Trim() == "")
{
return false;
}
if (File.Exists(fileName))
{
return true;
}
return false;
}
/// <summary>
/// 創(chuàng)建文件夾
/// </summary>
/// <param name="dirName"></param>
/// <returns></returns>
public static bool CreateDir(string dirName)
{
if (!Directory.Exists(dirName))
{
Directory.CreateDirectory(dirName);
}
return true;
}
/// <summary>
/// 創(chuàng)建文件
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static bool CreateFile(string fileName)
{
if (!File.Exists(fileName))
{
FileStream fs = File.Create(fileName);
fs.Close();
fs.Dispose();
}
return true;
}
/// <summary>
/// 讀文件內(nèi)容
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
public static string Read(string fileName)
{
if (!Exists(fileName))
{
return null;
}
//將文件信息讀入流中
using (FileStream fs = new FileStream(fileName, FileMode.Open))
{
return new StreamReader(fs).ReadToEnd();
}
}
public static string ReadLine(string fileName)
{
if (!Exists(fileName))
{
return null;
}
using (FileStream fs = new FileStream(fileName, FileMode.Open))
{
return new StreamReader(fs).ReadLine();
}
}
/// <summary>
/// 寫文件
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="content">文件內(nèi)容</param>
/// <returns></returns>
public static bool Write(string fileName, string content)
{
if (!Exists(fileName) || content == null)
{
return false;
}
//將文件信息讀入流中
using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate))
{
lock (fs)//鎖住流
{
if (!fs.CanWrite)
{
throw new System.Security.SecurityException("文件fileName=" + fileName + "是只讀文件不能寫入!");
}
byte[] buffer = Encoding.Default.GetBytes(content);
fs.Write(buffer, 0, buffer.Length);
return true;
}
}
}
/// <summary>
/// 寫入一行
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="content">內(nèi)容</param>
/// <returns></returns>
public static bool WriteLine(string fileName, string content)
{
using (FileStream fs = new FileStream(fileName, FileMode.OpenOrCreate | FileMode.Append))
{
lock (fs)
{
if (!fs.CanWrite)
{
throw new System.Security.SecurityException("文件fileName=" + fileName + "是只讀文件不能寫入!");
}
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(content);
sw.Dispose();
sw.Close();
return true;
}
}
}
public static bool CopyDir(DirectoryInfo fromDir, string toDir)
{
return CopyDir(fromDir, toDir, fromDir.FullName);
}
/// <summary>
/// 復(fù)制目錄
/// </summary>
/// <param name="fromDir">被復(fù)制的目錄</param>
/// <param name="toDir">復(fù)制到的目錄</param>
/// <returns></returns>
public static bool CopyDir(string fromDir, string toDir)
{
if (fromDir == null || toDir == null)
{
throw new NullReferenceException("參數(shù)為空");
}
if (fromDir == toDir)
{
throw new Exception("兩個(gè)目錄都是" + fromDir);
}
if (!Directory.Exists(fromDir))
{
throw new IOException("目錄fromDir="+fromDir+"不存在");
}
DirectoryInfo dir = new DirectoryInfo(fromDir);
return CopyDir(dir, toDir, dir.FullName);
}
/// <summary>
/// 復(fù)制目錄
/// </summary>
/// <param name="fromDir">被復(fù)制的目錄</param>
/// <param name="toDir">復(fù)制到的目錄</param>
/// <param name="rootDir">被復(fù)制的根目錄</param>
/// <returns></returns>
private static bool CopyDir(DirectoryInfo fromDir, string toDir, string rootDir)
{
string filePath = string.Empty;
foreach (FileInfo f in fromDir.GetFiles())
{
filePath = toDir + f.FullName.Substring(rootDir.Length);
string newDir = filePath.Substring(0, filePath.LastIndexOf("\\"));
CreateDir(newDir);
File.Copy(f.FullName, filePath, true);
}
foreach (DirectoryInfo dir in fromDir.GetDirectories())
{
CopyDir(dir, toDir, rootDir);
}
return true;
}
/// <summary>
/// 刪除文件
/// </summary>
/// <param name="fileName">文件的完整路徑</param>
/// <returns></returns>
public static bool DeleteFile(string fileName)
{
if (Exists(fileName))
{
File.Delete(fileName);
return true;
}
return false;
}
public static void DeleteDir(DirectoryInfo dir)
{
if (dir == null)
{
throw new NullReferenceException("目錄不存在");
}
foreach (DirectoryInfo d in dir.GetDirectories())
{
DeleteDir(d);
}
foreach (FileInfo f in dir.GetFiles())
{
DeleteFile(f.FullName);
}
dir.Delete();
}
/// <summary>
/// 刪除目錄
/// </summary>
/// <param name="dir">制定目錄</param>
/// <param name="onlyDir">是否只刪除目錄</param>
/// <returns></returns>
public static bool DeleteDir(string dir, bool onlyDir)
{
if (dir == null || dir.Trim() == "")
{
throw new NullReferenceException("目錄dir=" + dir + "不存在");
}
if (!Directory.Exists(dir))
{
return false;
}
DirectoryInfo dirInfo = new DirectoryInfo(dir);
if (dirInfo.GetFiles().Length == 0 && dirInfo.GetDirectories().Length==0)
{
Directory.Delete(dir);
return true;
}
if (!onlyDir)
{
return false;
}
else
{
DeleteDir(dirInfo);
return true;
}
}
/// <summary>
/// 在指定的目錄中查找文件
/// </summary>
/// <param name="dir">目錄</param>
/// <param name="fileName">文件名</param>
/// <returns></returns>
public static bool FindFile(string dir, string fileName)
{
if (dir == null || dir.Trim() == "" || fileName == null || fileName.Trim() == "" || !Directory.Exists(dir))
{
return false;
}
DirectoryInfo dirInfo = new DirectoryInfo(dir);
return FindFile(dirInfo, fileName);
}
public static bool FindFile(DirectoryInfo dir, string fileName)
{
foreach (DirectoryInfo d in dir.GetDirectories())
{
if (File.Exists(d.FullName + "\\" + fileName))
{
return true;
}
FindFile(d,fileName);
}
return false;
}
}
}