常用操作类-IO操作类

/// <summary>
    /// IO 操作类
    /// </summary>
    public class IOHelper
    {
        /// <summary>
        /// 创建目录
        /// </summary>
        /// <param name="dirName"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public static bool CreateDirectory(string dirName, string Name)
        {
            try
            {
                DirectoryInfo dirInfo = Directory.CreateDirectory(dirName + @"\" + Name);
                return dirInfo != null;
            }
            catch (Exception ex)
            {
                ServiceManager.Instance.LoggingService.Debug("创建目录失败" + ex.Message);
            }

            return false;
        }

        /// <summary>
        /// 输入路径名称获取绝对路径名称,如果绝对路径存在,直接返回原来的名称
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string GetAbsoluteFileName(string fileName)
        {
            if (System.IO.File.Exists(fileName))
            {
                return fileName;
            }

            return System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\" + fileName;
        }

        /// <summary>
        /// 获取绝对目录名
        /// </summary>
        /// <param name="pathName"></param>
        /// <returns></returns>
        public static string GetAbsoluteDirectoryName(string pathName)
        {
            if (System.IO.Directory.Exists(pathName))
            {
                return pathName;
            }

            return System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + @"\" + pathName;
        }

        /// <summary>
        /// 连接两个文件
        /// </summary>
        /// <param name="pathFirst"></param>
        /// <param name="pathSecond"></param>
        /// <returns></returns>
        public static string CombinePathName(string pathFirst, string pathSecond)
        {
            if (pathFirst.Length < 1)
            {
                return pathSecond;
            }

            if (pathSecond.Length < 1)
            {
                return pathFirst;
            }

            if (pathFirst[pathFirst.Length - 1] == '\\' ||
                pathFirst[pathFirst.Length - 1] == '/' ||
                pathSecond[0] == '\\' ||
                pathSecond[0] == '/'
                )
            {
                return pathFirst + pathSecond;
            }

            return pathFirst + "\\" + pathSecond;
        }

        /// <summary>
        /// 移动文件
        /// </summary>
        /// <param name="sourceFileName"></param>
        /// <param name="destFileName"></param>
        /// <returns></returns>
        public static bool MoveFile(string sourceFileName, string destFileName)
        {
            bool bOk = true;
            try
            {
                File.Move(sourceFileName, destFileName);
            }
            catch (Exception ex)
            {
                ServiceManager.Instance.LoggingService.Debug(ex);
                bOk = false;
            }
            return bOk;
        }

        /// <summary>
        /// 拷贝文件的方法
        /// </summary>
        /// <param name="srcFileName"></param>
        /// <param name="targetFileName"></param>
        /// <param name="overwrite"></param>
        /// <returns></returns>
        public static bool CopyFile(string srcFileName, string targetFileName, bool overwrite)
        {
            if (!File.Exists(srcFileName))
            {
                return false;
            }

            try
            {
                File.Copy(srcFileName, targetFileName, overwrite);
                return true;
            }
            catch (Exception ex)
            {
                ServiceManager.Instance.LoggingService.Debug("拷贝文件出现异常:[" + ex.ToString());
            }

            return false;
        }        

        /// <summary>
        /// 删除目录
        /// </summary>
        /// <param name="directoryName"></param>
        /// <returns></returns>
        public static bool DeleteDirectory(string directoryName)
        {
            if (!Directory.Exists(directoryName))
            {
                return true;
            }

            try
            {
                Directory.Delete(directoryName, true);
            }
            catch (Exception ex)
            {
                ServiceManager.Instance.LoggingService.Debug("删除目录失败:" + ex.Message);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static bool DeleteFile(string fileName)
        {
            if (!File.Exists(fileName))
            {
                return true;
            }

            try
            {
                File.Delete(fileName);
            }
            catch (Exception ex)
            {
                ServiceManager.Instance.LoggingService.Debug("删除文件失败:" + ex.Message);
                return false;
            }

            return true;
        }

        /// <summary>
        /// 拷贝文件或文件夹到剪贴板上
        /// </summary>
        /// <param name="pathArray"></param>
        /// <returns></returns>
        public static bool CopyFile2Clipboard(string[] pathArray)
        {
            if (pathArray == null)
            {
                return false;
            }

            StringCollection pathCollection = new StringCollection();

            foreach (string path in pathArray)
            {
                if (File.Exists(path) || Directory.Exists(path))
                {
                    pathCollection.Add(path);
                }
                else
                {
                    return false;
                }
            }

            try
            {
                Clipboard.SetFileDropList(pathCollection);
            }
            catch (Exception ex)
            {
                ServiceManager.Instance.LoggingService.Debug("复制出现错误:" + ex.Message);
                return false;
            }

            return true;
        }       
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值