场景

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. C++如何使用宏定义来简化代码性能测试 | cpp macro like function to implement a performance profiler

    本文首发于个人博客https://kezunlin.me/post/65dc693d/,欢迎阅读最新内容! cpp macro like function to implement a perform ...

  2. 母版页 treeview控件 SiteMapPath控件 treeview数据库绑定模式

     母版页就是网站中一样的部分母版页的后缀名是.Master可以把母版页当成一个页面  想让哪里是别的内容就可以  通过如下: <asp:ContentPlaceHolder ID="C ...

  3. 当h5页面图片加载失败后,给定一个默认图

    本文主要讨论页面中图片加载失败后替换默认图片的几种方式 重点来了:一定要记住error事件不冒泡. 相关的知识点:jquery的ready方法.$("img").error().i ...

  4. PAT 1009 Product of Polynomials 模拟

    This time, you are supposed to find A*B where A and B are two polynomials. Input Specification: Each ...

  5. ubuntu18.04 安装 QQ

    参照大佬文章https://www.lulinux.com/archives/1319 我将安装过程需要的命令行总结出来,便于直接快速安装. # 安装 wine git clone https://g ...

  6. 如何解决eclipse远程服务器上面的Rabbitmq连接超时问题?

    1.嗯,问题呢,就是一开始安装好RabbitMQ,练习了一下RabbitMQ的使用,但是呢,过了一段时间,我来复习的时候,发现运行出现下面的错误了.后来想想,是自己学习微服务的时候,修改了/etc/h ...

  7. 【HTML】---常用标签(1)

    Html常用标签(1) 重新整理学习下前端知识从Html标签开始.我们先看HTML 骨架格式: <!DOCTYPE html> <!--这句话就是告诉我们使用哪个html版本--&g ...

  8. Java学习 1.3——Java开发环境的搭建:安装JDK,配置环境变量

    了解了基本的Java知识后,就需要开始搭建开发环境了. 一,安装JDK JDK1.8下载地址 接受协议,选择选择自己的系统,我的是Windows64位: 点进去后会让你登录Oracle账号,没有就创建 ...

  9. docker redis使用

    启动方式一:docker默认启动redis 1.拉取镜像 docker pull redis:lastest (若不使用版本号,如docker pull redis,默认拉取最新镜像) 2.启动red ...

  10. Linux Shell之监测程序

    监测程序 一.探查进程 当程序运行在系统上时,我们称之为进程(process).想要监测这些进程,需要熟悉ps命令的用法.ps命令好比工具中的瑞士军刀,它能输出运行在系统上的所有程序的许多信息. 但是 ...