场景

C#中File类的常用读取与写入文件方法的使用:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/99693983

注:

博客主页:
https://blog.csdn.net/badao_liumang_qizhi
关注公众号
霸道的程序猿
获取编程相关电子书、教程推送与免费下载。

实现

获取文件的扩展名

        /// <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>
/// <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="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="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="filePathList">文件路径List</param>
/// <returns>文件是否全部存在</returns>
public static bool IsFilesExist(List<string> filePathList)
{
bool isAllExist = true;
foreach(string filePath in filePathList)
{
if(!File.Exists(filePath))
{
isAllExist = false;
}
}
return isAllExist;
}

创建目录

       /// <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 = ; 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 - ] != 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 = ; 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());
}
}

C#中对文件File常用操作方法的工具类的更多相关文章

  1. python基础:os模块中关于文件/目录常用的函数使用方法

    Python是跨平台的语言,也即是说同样的源代码在不同的操作系统不需要修改就可以同样实现 因此Python的作者就倒腾了OS模块这么一个玩意儿出来,有了OS模块,我们不需要关心什么操作系统下使用什么模 ...

  2. os模块中关于文件/目录常用的函数使用方法

    os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...

  3. 18 os/os.path模块中关于文件/目录常用的函数使用方法 (转)

    os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...

  4. C++中vector容器的常用操作方法实例总结

    C++中vector容器的常用操作方法实例总结 参考 1. C++中vector容器的常用操作方法实例总结: 完

  5. Android 开源控件与常用开发框架开发工具类

    Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...

  6. 常用的Java工具类——十六种

    常用的Java工具类——十六种 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选 ...

  7. 文件压缩、解压工具类。文件压缩格式为zip

    package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...

  8. Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类

    Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...

  9. android文件和图片的处理工具类(一)

    package com.gzcivil.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileO ...

随机推荐

  1. go语言之变量

    1.go的hello world package main //申明开头,表示代码所属包,申明为main包,可以编译为二进制程序 import ( "fmt" ) //导入格式化包 ...

  2. ABP开发框架前后端开发系列---(14)基于Winform的ABP快速开发框架

    前面介绍了很多ABP系列的文章,一步一步的把我们日常开发中涉及到的Web API服务构建.登录日志和操作审计日志.字典管理模块.省份城市的信息维护.权限管理模块中的组织机构.用户.角色.权限.菜单等内 ...

  3. ASP.NET图片上传和截取

    一.介绍:图片的上传直接使用ajax就可以了,截取图片的话使用到Jcrop插件. 图片上传资料:https://www.jb51.net/article/87654.htm 截取图片插件:http:/ ...

  4. wx-show与!show

    切换的表示 <!--index.wxml--> <view class="container"> <view class="item&quo ...

  5. 读写锁(ReadWriteLock)

    为了提高性能,Java提供了读写锁,读写锁分为读锁和写锁.多个读锁不互斥,读锁与写锁互斥,写锁与写锁互斥,这是由JVM控制的.如果没有写锁的情况下,读是无阻塞的,在一定程度上提高了程序的执行效率. 读 ...

  6. 读书笔记_python网络编程3_(1)

    0.前言 代码目录: https://github.com/brandon-rhodes/fopnp/tree/m/py3 0.1.网络实验环境:理解客户端与服务器是如何通过网络进行通信的 每台机器通 ...

  7. 小程序开发技术总结(wepy)

    创建wepy项目 全局安装或更新WePY命令行工具:npm install wepy-cli -g 在开发目录中生成Demo开发项目:wepy new myproject , 1.7.0之后的版本使用 ...

  8. GetPrivateProfileInt 使用方法

    GetPrivateProfileInt =>从ini文件取得数值 <参数> lpApplicationName String,指定在其中查找条目的小节.注意这个字串是不区分大小写的 ...

  9. CodeForces 1238C(思维+贪心)

    题意 https://vjudge.net/problem/CodeForces-1238C 您现在正在玩一个游戏,您初始在一个高度 h 的悬崖 悬崖沿壁高度为 1-h 的这些位置均有平台,平台有两种 ...

  10. RabbitMQ学习笔记(五、RabbitMQ集群)

    目录: RabbitMQ集群 镜像队列 RabbitMQ服务日志 RabbitMQ分布式部署 高可用集群 RabbitMQ集群: 1.集群中组件的状态 首先MQ一定要是一个高可用的中间件所以集群肯定是 ...