当前位置: 首页 > news >正文

全屋定制怎么营销太原优化排名推广

全屋定制怎么营销,太原优化排名推广,建公司网站要多久,wordpress文章内多页面C#实现封装常规文件操作的工具类FileHelper背景代码实现注意点背景 在不同项目开的发过程中,可能会用到许多相同或类似的功能开发或业务处理,从抽象设计的角度考虑,通常会把这些功能或业务操作进行封装,方便在不同项目中调用&…

C#实现封装常规文件操作的工具类FileHelper

  • 背景
  • 代码实现
  • 注意点

背景

在不同项目开的发过程中,可能会用到许多相同或类似的功能开发或业务处理,从抽象设计的角度考虑,通常会把这些功能或业务操作进行封装,方便在不同项目中调用,这也是重用思想的重要体现。本次分享的是在C#中实现常规文件操作(读写)的功能封装。

代码实现

FileHelper

using System;
using System.IO;
using System.Text;namespace Wongoing.Basic
{/// <summary>/// 文件操作类/// </summary>public class FileHelper : System.IDisposable{private bool _alreadyDispose;~FileHelper(){this.Dispose();}protected virtual void Dispose(bool isDisposing){if (this._alreadyDispose){return;}this._alreadyDispose = true;}public void Dispose(){this.Dispose(true);System.GC.SuppressFinalize(this);}/// <summary>/// 获取文件的扩展名/// </summary>/// <param name="filename">完整文件名</param>/// <returns>返回扩展名</returns>public static string GetPostfixStr(string filename){int num = filename.LastIndexOf(".");int length = filename.Length;return filename.Substring(num, length - num);}/// <summary>/// 向指定文件写入内容/// </summary>/// <param name="path">要写入内容的文件完整路径</param>/// <param name="content">要写入的内容</param>public static void WriteFile(string path, string content){try{object obj = new object();if (!System.IO.File.Exists(path)){System.IO.FileStream fileStream = System.IO.File.Create(path);fileStream.Close();}lock (obj){using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(path, false, System.Text.Encoding.Default)){streamWriter.WriteLine(content);streamWriter.Close();streamWriter.Dispose();}}}catch (System.Exception ex){ICSharpCode.Core.LoggingService<FileHelper>.Error(String.Format("写入文件{0}异常:{1}", path, ex.Message), ex);}}/// <summary>/// 向指定文件写入内容/// </summary>/// <param name="path">要写入内容的文件完整路径</param>/// <param name="content">要写入的内容</param>/// <param name="encoding">编码格式</param>public static void WriteFile(string path, string content, System.Text.Encoding encoding){try{object obj = new object();if (!System.IO.File.Exists(path)){System.IO.FileStream fileStream = System.IO.File.Create(path);fileStream.Close();}lock (obj){using (System.IO.StreamWriter streamWriter = new System.IO.StreamWriter(path, false, encoding)){streamWriter.WriteLine(content);streamWriter.Close();streamWriter.Dispose();}}}catch (System.Exception ex){ICSharpCode.Core.LoggingService<FileHelper>.Error(String.Format("写入文件{0}异常:{1}", path, ex.Message), ex);}}/// <summary>/// 读取文件内容/// </summary>/// <param name="path">要读取的文件路径</param>/// <returns>返回文件内容</returns>public static string ReadFile(string path){string result;if (!System.IO.File.Exists(path)){result = "不存在相应的目录";}else{System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.Default);result = streamReader.ReadToEnd();streamReader.Close();streamReader.Dispose();}return result;}/// <summary>/// 读取文件内容/// </summary>/// <param name="path">要读取的文件路径</param>/// <param name="encoding">编码格式</param>/// <returns>返回文件内容</returns>public static string ReadFile(string path, System.Text.Encoding encoding){string result;if (!System.IO.File.Exists(path)){result = "不存在相应的目录";}else{System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, encoding);result = streamReader.ReadToEnd();streamReader.Close();streamReader.Dispose();}return result;}/// <summary>/// 返回文件的完整路径/// </summary>/// <param name="fileName">文件名</param>/// <returns>返回文件的完整路径</returns>public static string getAppFileFullName(string fileName){return System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, fileName);}/// <summary>/// 从文件中读取第一行内容/// </summary>/// <param name="path">文件完整路径</param>/// <returns>返回文件第一行内容</returns>public static string GetFirstLine(string path){string result = string.Empty;if (!System.IO.File.Exists(path)){result = "不存在相应的目录";}else{System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.Default);result = streamReader.ReadLine();streamReader.Close();}return result;}/// <summary>/// 读取文件第二行内容/// </summary>/// <param name="path">文件完整路径</param>/// <returns>返回读取的内容</returns>public static string GetSecondLine(string path){string result = string.Empty;if (!System.IO.File.Exists(path)){result = "不存在相应的目录";}else{System.IO.FileStream stream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);System.IO.StreamReader streamReader = new System.IO.StreamReader(stream, System.Text.Encoding.Default);if (!string.IsNullOrEmpty(streamReader.ReadLine())){result = streamReader.ReadLine();}else{result = string.Empty;}streamReader.Close();}return result;}/// <summary>/// 读取文件中指定位置的值/// </summary>/// <param name="path">文件完整路径</param>/// <param name="row">要读取的第几行</param>/// <param name="col">要读取的第几列</param>/// <returns>返回读取的内容</returns>public static string GetValue(string path, int row, int col){string text = string.Empty;if (!System.IO.File.Exists(path)){return "不存在相应的目录";}string[] array;if (row == 1){text = FileHelper.GetFirstLine(path);array = text.ToString().Split(new char[]{':'});}else{text = FileHelper.GetSecondLine(path);array = text.ToString().Split(new char[]{':'});}return array[col];}/// <summary>/// 在文件末尾添加内容/// </summary>/// <param name="path">文件完整路径</param>/// <param name="content">要添加的内容</param>public static void FileAdd(string path, string content){try{object obj = new object();lock (obj){System.IO.StreamWriter streamWriter = System.IO.File.AppendText(path);streamWriter.Write(content);streamWriter.Flush();streamWriter.Close();}}catch (System.Exception ex){ICSharpCode.Core.LoggingService<FileHelper>.Error(String.Format("在文件{0}末尾添加内容异常:{1}", path, ex.Message), ex);}}/// <summary>/// 文件复制/// </summary>/// <param name="orignFile">源文件完整路径</param>/// <param name="newFile">目标文件完整路径</param>public static void FileCoppy(string orignFile, string newFile){System.IO.File.Copy(orignFile, newFile, true);}/// <summary>/// 删除文件/// </summary>/// <param name="path">要删除的文件的完整路径</param>public static void FileDel(string path){System.IO.File.Delete(path);}/// <summary>/// 文件移动(剪贴->粘贴)/// </summary>/// <param name="orignFile">源文件的完整路径</param>/// <param name="newFile">目标文件完整路径</param>public static void FileMove(string orignFile, string newFile){System.IO.File.Move(orignFile, newFile);}/// <summary>/// 创建目录/// </summary>/// <param name="orignFolder">当前目录</param>/// <param name="newFloder">要创建的目录名</param>public static void FolderCreate(string orignFolder, string newFloder){System.IO.Directory.SetCurrentDirectory(orignFolder);System.IO.Directory.CreateDirectory(newFloder);}/// <summary>/// 删除目录/// </summary>/// <param name="dir">要删除的目录</param>public static void DeleteFolder(string dir){if (System.IO.Directory.Exists(dir)){string[] fileSystemEntries = System.IO.Directory.GetFileSystemEntries(dir);for (int i = 0; i < fileSystemEntries.Length; i++){string text = fileSystemEntries[i];if (System.IO.File.Exists(text)){System.IO.File.Delete(text);}else{FileHelper.DeleteFolder(text);}}System.IO.Directory.Delete(dir);}}/// <summary>/// 目录内容复制/// </summary>/// <param name="srcPath">源目录</param>/// <param name="aimPath">目标目录</param>public static void CopyDir(string srcPath, string aimPath){try{if (aimPath[aimPath.Length - 1] != System.IO.Path.DirectorySeparatorChar){aimPath += System.IO.Path.DirectorySeparatorChar;}if (!System.IO.Directory.Exists(aimPath)){System.IO.Directory.CreateDirectory(aimPath);}string[] fileSystemEntries = System.IO.Directory.GetFileSystemEntries(srcPath);string[] array = fileSystemEntries;for (int i = 0; i < array.Length; i++){string text = array[i];if (System.IO.Directory.Exists(text)){FileHelper.CopyDir(text, aimPath + System.IO.Path.GetFileName(text));}else{System.IO.File.Copy(text, aimPath + System.IO.Path.GetFileName(text), true);}}}catch (System.Exception ex){throw new System.Exception(ex.ToString());}}}
}

注意点

因为我在封装的时候依赖了ICSharpCode.Core,如果您没有ICSharpCode,请把上面代码中

ICSharpCode.Core.LoggingService<FileHelper>.Error

改为

Console.WriteLine
http://www.lbrq.cn/news/2549035.html

相关文章:

  • 基于web的网站建设步骤互联网品牌的快速推广
  • 访问香港网站慢百度教育官网登录入口
  • 织梦网站会员上传图片电商网站建设哪家好
  • 哪些企业需要做网站广州推广引流公司
  • 企业所得税规避50种南京seo整站优化技术
  • 苏州设计网页网站软件开发自学步骤
  • 网站建设定制网站建设公司关键词排名手机优化软件
  • 想学网络营销网站建设福州网站建设方案外包
  • 外贸高端网站设计如何免费建立一个网站
  • 服装公司网站源码seo是搜索引擎吗
  • 深圳企业家日迈入第五年优化什么
  • 做网站开直通车营销宣传策划方案
  • 打渔网站建设新冠疫情最新消息今天
  • 宁波手机网站建设百度搜索引擎原理
  • 优客逸家网站源码北京seo课程培训
  • 中小企业网站建设好么网络广告策划案
  • 苏州北京网站建设sem竞价推广托管代运营公司
  • 购买云服务器后怎么做网站谷歌优化的网络公司
  • 好友介绍网站怎么做seo是什么化学名称
  • wordpress弹出登陆优化大师客服电话
  • 做网站 后端是谁来做的2023必考十大时政热点
  • 官方网站建设公司关键词排名点击软件网站
  • 建设网站的网站互联网平台
  • h5开发wordpress客户端优化关键词排名seo
  • 烟台广告公司网站建设河南做网站优化
  • 网站建设步骤详解视频免费行情软件网站下载大全
  • 企业主页的特点快速刷排名seo软件
  • php网站空间购买苏州seo网络推广
  • 用dreamweaver建设网站成品成功营销十大经典案例
  • 自己给公司做网站b站推广入口在哪
  • SpringAI智能客服Function Calling兼容性问题解决方案
  • 从入仓到结算全自动化:易境通如何重构散货拼柜业务流程?
  • 实战指南:如何将Git仓库中的特定文件夹及其历史完整迁移到另一个仓库
  • 可计算存储(Computational Storage)与DPU(Data Processing Unit)的技术特点对比及实际应用场景分析
  • Mac配置iterm2
  • 爬虫验证码处理:ddddocr 的详细使用(通用验证码识别OCR pypi版)