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

网站服务器建设seo网站关键词优化价格

网站服务器建设,seo网站关键词优化价格,天津建设工程信息网络,厚街外贸网站建设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/2671813.html

相关文章:

  • 网页制作与网站建设实战教程视频教程百度热搜关键词排行榜
  • 海口手机建站模板优就业seo课程学多久
  • 如何学习网站建设店铺推广怎么做
  • 阿里万网怎么做网站百度识图网页版
  • 网站怎么申请微信支付接口免费外链网站
  • dedecms修改网站教程百度广告客服电话
  • 合肥专业网站制作团队西地那非片的功能主治和副作用
  • 外贸高端网站设计网络营销包括
  • 颛桥做网站seo公司怎样找客户
  • 什么网站做设计可以赚钱网站排名优化公司哪家好
  • 有哪些网站可以做外贸批发seo怎样优化网站
  • 工程建设科学技术奖申报网站免费seo课程
  • 做b2c网站需要多少钱神童预言新冠2023结束
  • 网站维护升级页面外贸新手怎样用谷歌找客户
  • 新冠政策最新20条长沙seo网站推广
  • 宅男做网站宁波优化网站排名软件
  • wordpress 网站提速百度快照和广告的区别
  • 网站如何优化推广新东方教育机构官网
  • 如何制作简单网站百度灰色关键词代发
  • 安阳百度网站制作多少钱淘宝app官方下载
  • 西安微信网站开发站长工具seo综合查询分析
  • 做外贸一般上哪些网站泰安网站seo推广
  • 跨境电商网站建设主管岗位职责中央电视台一套广告价目表
  • [ 1500元做网站_验收满意再付款! 推推蛙品牌策划
  • 做兼职的网站都有哪些企业管理培训课程网课
  • 高端网站建设天软科技关键词优化按天计费
  • 银川住房和城乡建设部网站济南网站优化排名推广
  • 哪些做任务可以赚钱的网站网站怎么推广
  • 网站空间香港今天的重要新闻
  • 祥云网站建设百度关键词排名怎么做
  • FinQ4Cn: 基于 MCP 协议的中国 A 股量化分析
  • macOS 搭建 Gitea 私有 Git 服务器教程
  • python魔法属性__doc__介绍
  • 性能解析案例
  • Flask多进程数据库访问问题详解
  • MySQL 子查询