文本文件的操作

来源:互联网 发布:淘宝违规商品下架 编辑:程序博客网 时间:2024/06/02 07:28
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;


/// <summary>
/// 文件操作
/// </summary>
public class path
{
    #region 写入文件


    /// <summary>
    /// 写入文件 ,不存在直接创建
    /// </summary>
    /// <param name="patha">路径(包括文件名)</param>
    /// <param name="content">内容</param>
    public static void pathwrite(string path,string content)
    {
        File.AppendAllText(path, content); //path:表示路径


    }
    #endregion


    #region 判断文件是否存在


    /// <summary>
    /// 判断文件是否存在
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static Boolean pathexextis(string path)
    {
        if (File.Exists(path) == true)
        {
            return true;
        }
        return false;
    }


    #endregion


    #region 删除文件


    /// <summary>
    /// 删除文件,删除前最好先判断一下文件是否存在
    /// </summary>
    /// <param name="path">文件路径</param>
    /// <returns></returns>
    public static Boolean deletepath(string path)
    {
        try
        {
            File.Delete(path);
            return true;
        }
        catch(Exception ex)
        {
            return false;
            throw(ex);
        }
        
      
    }


    #endregion


    #region 创建文件夹


    /// <summary>
    /// 创建文件夹
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static Boolean createpath(string path)
    {
        try
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(path); //路径不存在的会自动创建
            directoryInfo.Create();
            return true;
        }
        catch(Exception ex)
        {
            return false;
        }
       
    }


    #endregion


    #region 判断文件夹是否存在


    /// <summary>
    /// 判断文件夹是否存在
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static Boolean pathextise(string path)
    {
        if(Directory.Exists(path)==true)
        {
            return true;
        }
        return false;
    }


    #endregion


    #region 删除文件夹


    /// <summary>
    ///删除文件夹
    /// </summary>
    /// <param name="path"></param>
    /// <returns></returns>
    public static Boolean createpath(string path)
    {
        try
        {
            DirectoryInfo directoryInfo = new DirectoryInfo(path); 
            directoryInfo.Delete(true);//true表示允许删除文件夹,删除掉文件夹下的所有文件包括子目录
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }


    }


    #endregion


    #region 拷贝文件


    /// <summary>
    /// 拷贝文件
    /// </summary>
    /// <param name="path"></param>
    /// <param name="destpath"></param>
    /// <returns></returns>
   public static Boolean copypath(string path,string destpath)
   {
       try
       {
           File.Copy(path, destpath);         //path代表要复制的目录,destpath代表目的地
           return true;
       }
       catch (Exception ex)
       {
           return false;
       }
      
   }


    #endregion


    #region 获取文件大小


    /// <summary> 
    /// 获取文件大小 
    /// </summary> 
    /// <param name="txtPath">文件地址</param> 
    /// <returns></returns> 
    public static double GetFileSize(string txtPath) 
    { 
        try 
        { 
            if (File.Exists(txtPath) == true) 
            { 
            FileStream fs = new FileStream(txtPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); 
            long len = fs.Length; 
            fs.Dispose(); 
            return len; 
            } 
            return 0; 
        } 
        catch 
        { 
        return 0; 
        } 
    } 


    #endregion 


    #region 取指定文件的后缀名
 
    /// <summary> 
    /// 取指定文件的后缀名 
    /// </summary> 
    /// <param name="PicName">文件名或文件路径</param> 
    /// <returns></returns> 
    public static string GetPicType(string PicName) 
    { 
        return Path.GetExtension(PicName); 
    } 


    #endregion 


}
         
原创粉丝点击