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 ...
随机推荐
- Springboot对SpringMVC如何扩展配置
原文地址:http://www.javayihao.top/detail/171 概述 Springboot在web层的开发基本都是采用Springmvc框架技术,但是Springmvc中的某些配置在 ...
- Mybatis1
一.MyBatis介绍 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis ...
- Selenium(七):选择框(radio框、checkbox框、select框)
1. 选择框 本章使用的html代码: <!DOCTYPE html> <html lang="en"> <head> <meta cha ...
- Vue.js+vue-element搭建属于自己的后台管理模板:创建一个项目(四)
Vue.js+vue-element搭建属于自己的后台管理模板:创建一个项目(四) 前言 本章主要讲解通过Vue CLI 脚手架构建工具创建一个项目,在学习Vue CLI之前我们需要先了解下webpa ...
- 1-5-JS基础-数组应用及实例应用
array 数组 一般简写arr 格式 var arr [ '第1个','第2个','第3个','第4个' ] 最后一个不要叫逗号 alert(arr.length) 弹出数组长度 4个 alert( ...
- mssql sqlserver text、image字段类型无法使用DISTINCT的处理方法分享
转自: http://www.maomao365.com/?p=9775 摘要: 下文简述sqlserver数据库中 text image类型无法使用distinct的处理方法分享 实验环境:sql ...
- Centos7.6 Apache 虚拟机禁止访问IP,允许访问多域名多个虚拟目录设置
存放配置文件路径 /etc/httpd/conf.d自定义命名,配置文件后缀格式确保 *.conf注:这是两个配置文件,也是两种方法. # 一个WEB目录# 禁止以IP地址形式访问<Virtua ...
- vue项目中引入特殊字体
特殊字体指的是默认电脑中没有的 1.下载字体 2.新建文件夹font,把字体放进去 3.新建font.css 4.使用@font-face设置字体 @font-face { font-family: ...
- poj 3233 矩阵快速幂
地址 http://poj.org/problem?id=3233 大意是n维数组 最多k次方 结果模m的相加和是多少 Given a n × n matrix A and a positive i ...
- SpringMVC重定向路径中带中文参数
SpringMVC重定向路径中带中文参数 springboot重定向到后端接口测试 package com.mozq.http.http_01.demo; import org.springframe ...