不灬赖

自律>>自由>>自信

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

public static class FileOperateHelp
    {
        /// <summary>
        /// 写
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string Write(string fileName, string content)
        {
            try
            {
                var encoding = Encoding.GetEncoding("utf-8");
                var file = PathConvert(fileName);
                var streamWriter = new StreamWriter(file, false, encoding);
                streamWriter.Write(content);
                streamWriter.Flush();
                streamWriter.Close();
                streamWriter.Dispose();
                Console.WriteLine("Write succeed");
                return "OK";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return ex.Message;
            }
        }

        /// <summary>
        /// 读
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string Read(string fileName)
        {
            try
            {
                var encoding = Encoding.GetEncoding("utf-8");
                var file = PathConvert(fileName);
                if (File.Exists(file))
                {
                    var streamReader = new StreamReader(file,encoding);
                    var readResult = streamReader.ReadToEnd();
                    streamReader.Close();
                    streamReader.Dispose();
                    Console.WriteLine(readResult);
                    return readResult;
                }

                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return e.Message;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static string DeleteFile(string fileName)
        {
            try
            {
                var path = PathConvert(fileName);
                File.Delete(path);
                Console.WriteLine("Delete succeed");
                return "Ok";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return ex.Message;
            }
        }

        /// <summary>
        /// 移动
        /// </summary>
        /// <param name="originFile"></param>
        /// <param name="newFile"></param>
        /// <returns></returns>
        public static string RemoveFile(string originFile,string newFile)
        {
            try
            {
                originFile = PathConvert(originFile);
                newFile = PathConvert(newFile);
                File.Move(originFile,newFile);
                Console.WriteLine("Remove succeed");
                return "OK";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        /// <summary>
        /// 递归删除文件夹下文件
        /// </summary>
        /// <param name="dir"></param>
        /// <returns></returns>
        public static string DeleteFolder(string dir)
        {
            try
            {
                dir = PathConvert(dir);
                if (Directory.Exists(dir))
                {
                    foreach (var d in Directory.GetFileSystemEntries(dir))
                    {
                        if (File.Exists(d))
                        {
                            File.Delete(d);
                        }
                        else
                        {
                            DeleteFolder(d);
                        }
                        Directory.Delete(dir,true);
                    }
                }
                Console.WriteLine("Delete Dir succeed");
                return "OK";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        /// <summary>
        /// 将相对路径转换成绝对路径
        /// </summary>
        /// <param name="strPath"></param>
        /// <returns></returns>
        private static string PathConvert(string strPath)
        {
            strPath = strPath.Replace("/","\\");
            if (strPath.StartsWith("\\"))
            {
                strPath = strPath.TrimStart('\\');
            }

            return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, strPath);
        }
    }

posted on 2020-11-12 14:42  不灬赖  阅读(97)  评论(0编辑  收藏  举报