场景

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. PAT 1010 Radix 进制转换+二分法

    Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 110 be true? The an ...

  2. DataTable实现分组

    有时候我们从数据库中查询出来数据之后,需要按照DataTable的某列进行分组,可以使用下面的方法实现,代码如下: using System; using System.Collections.Gen ...

  3. ASP.NET Core 2.2 WebApi 系列【七】泛型仓储模式和工作单元

    在之前的泛型仓储模式实现中,每个增删改都调用了SaveChanges方法,导致每次更新都提交了事务. 在实际开发过程中,我们经常遇到同时操作多张表数据,那么按照之前的写法,对数据库提交了多次操作,开启 ...

  4. PHP发送短信

    1.要拼接接收的手机号和短信 public function sendcode() { $parpm = input(); $valist = $this->validate($parpm, [ ...

  5. js写个小时钟

    原生js写个小时钟 一.代码 今天美化博客园自学的哈,分享一下 <!--标题变成时钟--> <div id="Header1_HeaderTitle">&l ...

  6. OpenCV:图像的水平、垂直、水平垂直翻转

    首先导入相关的库: import cv2 import matplotlib.pyplot as plt 自定义展示图片的函数: def show(image): plt.imshow(image) ...

  7. JS 逻辑

    JS 逻辑 Boolean(逻辑)对象用于将非逻辑值转换为逻辑值(true 或者 false). Boolean 对象 您可以将 Boolean 对象理解为一个产生逻辑值的对象包装器. Boolean ...

  8. Spring Boot 2 + jpa + mysql例子

    Spring Data框架为数据访问提供了一个通用的模型,无论访问哪种数据库,都可以使用同样的方式,主要有以下几个功能:(1)提供数据与对象映射的抽象层,同一个对象,可以被映射为不同数据库的数据:(2 ...

  9. Data Management Technology(2) -- Data Model

    1.Data Model Model Is the abstraction of real world Reveal the essence of objects, help people to lo ...

  10. Linux和Windows的区别

    1. 软件与支持 • Windows 平台:数量和质量的优势,不过大部分为收费软件:由微软官方提供重要支持和服务: • Linux 平台:大都为开源自由软件,用户可以修改定制和再发布,由于基本免费没有 ...