C#中对文件File常用操作方法的工具类
场景
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常用操作方法的工具类的更多相关文章
- python基础:os模块中关于文件/目录常用的函数使用方法
Python是跨平台的语言,也即是说同样的源代码在不同的操作系统不需要修改就可以同样实现 因此Python的作者就倒腾了OS模块这么一个玩意儿出来,有了OS模块,我们不需要关心什么操作系统下使用什么模 ...
- os模块中关于文件/目录常用的函数使用方法
os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...
- 18 os/os.path模块中关于文件/目录常用的函数使用方法 (转)
os模块中关于文件/目录常用的函数使用方法 函数名 使用方法 getcwd() 返回当前工作目录 chdir(path) 改变工作目录 listdir(path='.') 列举指定目录中的文件名('. ...
- C++中vector容器的常用操作方法实例总结
C++中vector容器的常用操作方法实例总结 参考 1. C++中vector容器的常用操作方法实例总结: 完
- Android 开源控件与常用开发框架开发工具类
Android的加载动画AVLoadingIndicatorView 项目地址: https://github.com/81813780/AVLoadingIndicatorView 首先,在 bui ...
- 常用的Java工具类——十六种
常用的Java工具类——十六种 在Java中,工具类定义了一组公共方法,这篇文章将介绍Java中使用最频繁及最通用的Java工具类.以下工具类.方法按使用流行度排名,参考数据来源于Github上随机选 ...
- 文件压缩、解压工具类。文件压缩格式为zip
package com.JUtils.file; import java.io.BufferedOutputStream; import java.io.File; import java.io.Fi ...
- Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类
Java 通过Xml导出Excel文件,Java Excel 导出工具类,Java导出Excel工具类 ============================== ©Copyright 蕃薯耀 20 ...
- android文件和图片的处理工具类(一)
package com.gzcivil.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileO ...
随机推荐
- Linux下MySQL数据库的my.cnf配置文件,解决中文乱码问题
系统 CentOS 7.7 MySQL - 5.7.28文件放置目录:/etc/文件权限:644解决问题:存储中文数据乱码 # For advice on how to change settings ...
- 01-Java类加载机制详解
类的加载过程 在使用java命令运行主类(main)的时候,首先要通过类加载器将类加载到JVM内存中去.主类在运行过程中如果用到其他的类就会逐步加载这些类.jar包里的类并不是一次性加载的,是使用的时 ...
- js 导航栏多项点击显示下拉菜单代码
<!DOCTYPE html> <html> <head> <title>Dropdown</title> <!--<link ...
- BPC成员公式
BPC可以通过成员公式,定义维度成员之间相关的计算公式,前端自动得到相应计算结果. 新建成员公式,选择对应的维度成员. 编辑维度成员的计算公式.保存后激活维度即可.
- kali安装vmtool后依旧无法拖拽文件,复制粘贴,解决办法
本文链接:https://blog.csdn.net/Key_book/article/details/80310235命令行下 执行 apt-get install open-vm-tools-de ...
- SQL Server有意思的数据类型隐式转换问题
写这篇文章的时候,还真不知道如何取名,也不知道这个该如何将其归类.这个是同事遇到的一个案例,案例比较复杂,这里抽丝剥茧,仅仅构造一个简单的案例来展现一下这个问题.我们先构造测试数据,如下所示: CRE ...
- Makefile 文件格式;makefile伪目标
Makefile包含 目标文件.依赖文件.可运行命令三部分. 每部分的基本格式例如以下: test: prog.o code.o gcc -o test prog.o code.o 当中 ...
- Java学习笔记(5)--- Number类和Math 类,String类的应用,Java数组入门
1.Number 和 Math 类: 在实际开发过程中,我们经常会遇到需要使用对象,而不是内置数据类型(int,double,float这些)的情形. 这种由编译器特别支持的包装称为装箱,所以当内置数 ...
- 你的学习方法怎么样?IT的学习方法应该是什么-Dotest
OK,自从你打开这个文章,那么一定跟我有类似的困惑. 建议1)IT的东西没有背诵的,要做.要做,一定要动手做: 2)讨论.讨论,一定要多讨论.在讨论过程中,以往的不理解问题,可能就迎刃而解了: 3)知 ...
- Cisco pppoe上网设置
1.配置虚拟端口: interface Dialer1 ip address negotiated ip nat outside ip virtual-reassembly in encapsulat ...