控件中国网现已改版,您看到的是老版本网站的镜像,系统正在为您跳转到新网站首页,请稍候.......
中国最专业的商业控件资讯网产品咨询电话:023-67870900 023-67871946
产品咨询EMAIL:SALES@COMPONENTCN.COM

常用操作文件DLL类

作者:佚名 出处:互联网 2011年05月27日 阅读:

常用操作文件DLL类

 /// <summary>
    /// 关于文件的所有操作类
    /// </summary>
    public class FileOption
    {
       
/// <summary>
/// 是否删除成功
/// </summary>
/// <param name="FileFullPath">删除文件全路径</param>
/// <returns>是否删除成功</returns>
        public bool DeleteFile(string FileFullPath)
        {
            try
            {
                if (File.Exists(FileFullPath) == true)
                {
                    if (File.GetAttributes(FileFullPath) == FileAttributes.Normal)
                    {
                        File.Delete(FileFullPath);
                    }
                    else
                    {
                        File.SetAttributes(FileFullPath, FileAttributes.Normal);
                        File.Delete(FileFullPath);
                    }
                }
                return true;
            }
            catch
            {
                return false;
               
              
            }
        }

/// <summary>
/// 得到文件名
/// </summary>
/// <param name="FileFullPath"></param>
/// <returns></returns>
        public string GetFileName(string FileFullPath)
        {
            if (File.Exists(FileFullPath))
            {

                FileInfo file = new FileInfo(FileFullPath);
                return file.Name;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到文件
        /// </summary>
        /// <param name="FileFullPath"></param>
        /// <param name="IncludeExtention"></param>
        /// <returns></returns>
        public string GetFileName(string FileFullPath, bool IncludeExtention)
        {
            if (File.Exists(FileFullPath))
            {
                FileInfo fileinfo=new FileInfo(FileFullPath);

                if (IncludeExtention)
                {
                    return fileinfo.Name;
                }
                else
                {
                    return fileinfo.Name.Replace(fileinfo.Extension, "");
                }
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到扩展名
        /// </summary>
        /// <param name="FileFullPath"></param>
        /// <returns></returns>
        public String GetFileExtent(string FileFullPath)
        {
            if (File.Exists(FileFullPath))
            {

                FileInfo file = new FileInfo(FileFullPath);
                return file.Extension;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 是否打开文件
        /// </summary>
        /// <param name="FileFullPath"></param>
        /// <returns></returns>
        public bool OpentFile(string FileFullPath)
        {
            if (File.Exists(FileFullPath))
            {
                System.Diagnostics.Process.Start(FileFullPath);
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到文件大小
        /// </summary>
        /// <param name="FileFullPath"></param>
        /// <returns></returns>
        public string GetFileSize(string FileFullPath)
        {
            if (File.Exists(FileFullPath))
            {
                FileInfo fileInfo = new FileInfo(FileFullPath);
                long fileLength = fileInfo.Length;
                if (fileLength > 1024 * 1024 * 1024)
                {
                    return Math.Round((fileLength + 0.0) / (1024 * 1024*1024), 2).ToString()+"G";
                }
                else if (fileLength > 1024 * 1024)
                {
                    return Math.Round((fileLength + 0.0) / (1024 * 1024), 2).ToString() + "m";
                }
                else
                {
                    return Math.Round((fileLength + 0.0) / (1024), 2).ToString() + "kb";
                }
              
            }
            else
            {
                return "0";
            }
        }

 


        /// <summary>
        /// 将文件转变为字节
        /// </summary>
        /// <param name="FileFullPath"></param>
        /// <returns></returns>
        public byte[] FileToStreanByte(string FileFullPath)
        {
            byte[] fileData = null;
            if (File.Exists(FileFullPath))
            {
                FileStream fs = new FileStream(FileFullPath, FileMode.OpenOrCreate);
                fileData = new byte[fs.Length];
                fs.Read(fileData, 0, fileData.Length);
                fs.Close();
            }
            return fileData;
        }


        /// <summary>
        ///
        /// </summary>
        /// <param name="FileFullPath"></param>
        /// <param name="Bytesfile"></param>
        /// <returns></returns>
        public bool ByteToFileStrean(string FileFullPath,byte[] Bytesfile)
        {
            try
            {
                if (File.Exists(FileFullPath))
                {
                    File.Delete(FileFullPath);
                }
                FileStream fs = File.Create(FileFullPath);
                fs.Write(Bytesfile, 0, Convert.ToInt32(fs.Length));
                fs.Close();
                return true;
            }
            catch
            {

                return false;
            }
        }

        /// <summary>
        /// 序列化
        /// </summary>
        /// <param name="FileFullPath"></param>
        /// <returns></returns>
        public bool SerializeXmlFile(string FileFullPath)
        {
            try
            {
                System.Data.DataSet DS = new System.Data.DataSet();
                DS.WriteXml(FileFullPath);
                FileStream FS = new FileStream(FileFullPath + ".TMP", FileMode.OpenOrCreate);
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter BF = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                BF.Serialize(FS, DS);
                FS.Close();
                DeleteFile(FileFullPath);
                File.Move(FileFullPath + ".TMP", FileFullPath);
                return true;
            }
            catch
            {

                return false;
            }
        


       
        }


        /// <summary>
        /// 反序列XML
        /// </summary>
        /// <param name="FileFullPath"></param>
        /// <returns></returns>
        public bool DeserialzeXmlFile(string FileFullPath)
        {
            try
            {
                System.Data.DataSet ds = new System.Data.DataSet();
                FileStream FS = new FileStream(FileFullPath, FileMode.OpenOrCreate);
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter BF = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                ((System.Data.DataSet)BF.Deserialize(FS)).WriteXml(FileFullPath + ".TMP");
                FS.Close();
                DeleteFile(FileFullPath);
                File.Move(FileFullPath + ".TMP", FileFullPath);
                return true;
            }
            catch
            {

                return false;
            }
        }

 

 

 

 


    }

 

热推产品

  • ActiveReport... 强大的.NET报表设计、浏览、打印、转换控件,可以同时用于WindowsForms谀坔攀戀Forms平台下......
  • AnyChart AnyChart使你可以创建出绚丽的交互式的Flash和HTML5的图表和仪表控件。可以用于仪表盘的创......
首页 | 新闻中心 | 产品中心 | 技术文档 | 友情连接 | 关于磐岩 | 技术支持中心 | 联系我们 | 帮助中心 Copyright-2006 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 电话:023 - 67870900 传真:023 - 67870270 产品咨询:sales@componentcn.com 渝ICP备12000264号 法律顾问:元炳律师事务所 重庆市江北区塔坪36号维丰创意绿苑A座28-5 邮编:400020
在线客服
在线客服系统
在线客服
在线客服系统