system.io命名空间下
看看帮助

解决方案 »

  1.   

    public class FileOperate
    {
    string sFileName = "";
    #region 构造函数
    public FileOperate(){}
    public FileOperate(string strFileName)
    {
    sFileName = strFileName;
    }
    #endregion #region 文件操作
    #region IsFileExists--检验文件是否存在(Create at 2004-04-16 by Windy)
    /// <summary>
    /// 检验文件是否存在。
    /// </summary>
    /// <param name="strPath">要检查的文件</param>
    /// <returns>bool</returns></returns>
    public bool IsFileExists(string strPath)
    {
    return File.Exists(strPath);
    }
    #endregion #region CreateFile--创建文件(Create at 2004-04-16 by Windy)
    /// <summary>
    /// 创建文件。
    /// </summary>
    /// <param name="strPath">文件名</param>
    public void CreateFile(string strPath)
    {
    if(!IsFileExists(strPath))
    {
    File.Create(strPath);
    }
    }
    #endregion #region DeleteFile--删除文件 (Create at 2004-04-16 by Windy)
    /// <summary>
    /// 删除文件。
    /// </summary>
    /// <param name="strFileName">要删除文件的名称</param>
    public void DeleteFile(string strFileName)
    {
    try
    {
    //检验文件是否存在
    if(IsFileExists(strFileName))
    {
    File.Delete(strFileName);
    }
    else
    {
    throw new FileNotFoundException("文件[ " + strFileName + " ]不存在");
    }
    }
    catch(Exception e1)
    {
    throw e1;
    }
    }
    #endregion #region MoveFile--移动文件(Create at 2004-04-16 by Windy)
    /// <summary>
    /// 移动文件。
    /// </summary>
    /// <param name="strSourceFileName">要移动的文件</param>
    /// <param name="strDestFileName">目标文件</param>
    public void MoveFile(string strSourceFileName,string strDestFileName)
    {
    try
    {
    if(!IsFileExists(strSourceFileName)){throw new FileNotFoundException("原文件不存在!!");}
    if(IsFileExists(strDestFileName))
    {
    if(MessageBox.Show("目标文件已存在,\r\n您要覆盖此文件吗?","Question!",
    MessageBoxButtons.YesNo,
    MessageBoxIcon.Question,
    MessageBoxDefaultButton.Button1) == DialogResult.Yes)
    {
    File.Delete(strDestFileName);
    File.Move(strSourceFileName,strDestFileName);
    }
    }
    else
    {
    File.Move(strSourceFileName,strDestFileName);
    }
    }
    catch(Exception e1)
    {
    throw e1;
    }
    }
    #endregion #region CopyFile--拷贝文件(Create at 2004-04-16 by Windy)
    /// <summary>
    /// 拷贝文件。
    /// </summary>
    /// <param name="strSourceFileName">要复制的文件</param>
    /// <param name="strDestFileName">目标文件,它不能是一个目录或现有文件</param>
    public void CopyFile(string strSourceFileName,string strDestFileName)
    {
    try
    {
    if(!IsFileExists(strSourceFileName)){throw new FileNotFoundException("原文件[ " + strSourceFileName + " ]不存在!!");}
    if(IsFileExists(strDestFileName))
    {
    if(MessageBox.Show("目标文件已存在,\r\n您要覆盖此文件吗?","Question!",
    MessageBoxButtons.YesNo,
    MessageBoxIcon.Question,
    MessageBoxDefaultButton.Button1) == DialogResult.Yes)
    {
    File.Copy(strSourceFileName,strDestFileName,true);
    }
    }
    else
    {
    File.Copy(strSourceFileName,strDestFileName);
    }
    }
    catch(Exception e1)
    {
    throw e1;
    }
    }
    #endregion #region CapAttribute--获取文件或目录的属性(Create at 2004-04-20 by Windy)
    /// <summary>
    /// 获取文件或目录的属性
    /// </summary>
    /// <param name="strFileName">文件或目录</param>
    /// <returns>FileAttributes(enum)</returns>
    public FileAttributes CapAttribute(string strFileName)
    {
    //检验文件是否存在
    if(!IsFileExists(strFileName)){throw new Exception("该文件不存在!");}
    FileAttributes fa = File.GetAttributes(strFileName);
    return fa;
    }
    #endregion
    #endregion #region 目录操作
    #region IsDirectoryExists--检验目录是否存在(Create at 2004-04-19 by Windy)
    /// <summary>
    /// 检验目录是否存在
    /// </summary>
    /// <param name="strPath">要检验的目录路径</param>
    /// <returns>bool</returns>
    private bool IsDirectoryExists(string strPath)
    {
    DirectoryInfo dir = new DirectoryInfo(strPath);
    return dir.Exists;
    }
    #endregion #region IsDirectoryEmpty--检验目录是否为空(Create at 2004-04-19 by Windy)
    /// <summary>
    /// 检验目录是否为空
    /// </summary>
    /// <param name="strPath">要检验的目录路径</param>
    /// <returns>bool</returns>
    public bool IsDirectoryEmpty(string strPath)
    {
    try
    {
    DirectoryInfo dir = new DirectoryInfo(strPath);
    //检验该目录下是否有文件存在
    FileInfo[] fi = dir.GetFiles();
    if(fi.Length > 0){return false;}
    //检验该目录下是否有子目录存在
    DirectoryInfo[] di = dir.GetDirectories();
    if(di.Length > 0){return false;} return true;
    }
    catch(Exception e1)
    {
    throw e1;
    }
    }
    #endregion #region CreateDirectry--创建目录(Create at 2004-04-19 by Windy)
    /// <summary>
    /// 创建目录
    /// </summary>
    /// <param name="strPath">要创建的目录路径</param>
    public void CreateDirectry(string strPath)
    {
    try
    {
    if(IsDirectoryExists(strPath))
    {
    throw new Exception("该目录已经存在!无法创建!");
    }
    DirectoryInfo dir = new DirectoryInfo(strPath);
    dir.Create();
    }
    catch(Exception e1)
    {
    throw e1;
    }
    }
    #endregion #region DeleteDirectory--删除目录(Create at 2004-04-19 by Windy)
    /// <summary>
    /// 删除目录
    /// </summary>
    /// <param name="strPath">要删除的目录路径</param>
    public void DeleteDirectory(string strPath)
    {
    try
    {
    DirectoryInfo di = new DirectoryInfo(strPath);
    //检验该目录是否为空
    if(IsDirectoryEmpty(strPath)){di.Delete();}
    else
    {
    if(MessageBox.Show("该目录不为空,确定要删除它吗?\n这将同时删除他下面的文件和子目录",
    "确定?",
    MessageBoxButtons.YesNo,
    MessageBoxIcon.Question,
    MessageBoxDefaultButton.Button2) == DialogResult.Yes)
    {
    di.Delete(true);
    }
    }
    }
    catch(Exception e1)
    {
    throw e1;
    }
    }
    #endregion #region MoveDirectory--移动目录(Create at 2004-04-20 by Windy)
    /// <summary>
    /// 移动目录,原路径和目的路径必须不同名.
    /// 当原路径和目的路径在同一个目录下,则此操作等于更改目录名
    /// </summary>
    /// <param name="strPath">要移动的目录</param>
    /// <param name="strDesPath">移动后的目录名</param>
    public void MoveDirectory(string strSouPath,string strDesPath)
    {
    try
    {
    //检验目录是否存在
    if(!IsDirectoryExists(strSouPath))
    {
    throw new Exception("要移动的目录不存在!");
    }
    DirectoryInfo di = new DirectoryInfo(strSouPath);
    di.MoveTo(strDesPath);
    }
    catch(Exception e1)
    {
    throw e1;
    }
    }
    #endregion
    #endregion public void WriteLine(string content)
    {
    StreamWriter sw = File.CreateText(sFileName);
    sw.Write(content);
    sw.Flush();
    sw.Close();
    }
    }
      

  2.   

    看看MSDN中的File和FileInfo类,里面说的很详细