Net文件递归查找并保存
//获取文件夹下的所有文件 并保存
string[] path = Directory.GetFiles(NeedFilePath, "*.*");
//获取文件夹下的所有子文件
string[] files = Directory.GetDirectories(NeedFilePath);
代码如下:
/// <summary>
/// 文件递归
/// </summary>
/// <param name="NeedFilePath">需要转化的文件路径</param>
/// <param name="CopyToFilePath">指定生成的文件路径</param>
protected void CreatePath(string NeedFilePath, string CopyToFilePath)
{
string FilePath = CopyToFilePath;
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}
//获取文件夹下的所有文件 并保存
string[] path = Directory.GetFiles(NeedFilePath, "*.*");
string newfilepath = "";
int Count = ;
foreach (string vale in path)
{
Count = ;
Count = vale.Split('\\').Length;
newfilepath = FilePath + "\\" + vale.Split('\\')[Count - ];
FileHelper.CreateFile(newfilepath);
FileHelper.FileCoppy(vale, newfilepath);
}
//获取文件夹下的所有子文件
string[] files = Directory.GetDirectories(NeedFilePath);
foreach (string vale in files)
{
Count = vale.Split('\\').Length;
//递归文件夹
CreatePath(vale, CopyToFilePath + "\\" + vale.Split('\\')[Count - ]);
}
}
FileHelper类:
public static class FileHelper
{
/// <summary>
/// 写文件
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="content">文件内容</param>
/// <param name="encoding">指定文件编码</param>
public static void Write_Txt(string fileName, string content, string encoding)
{
if (string.IsNullOrEmpty(fileName))
{
throw new ArgumentNullException(fileName);
}
if (string.IsNullOrEmpty(content))
{
throw new ArgumentNullException(content);
}
if (string.IsNullOrEmpty(encoding))
{
throw new ArgumentNullException(encoding);
}
var code = Encoding.GetEncoding(encoding);
var htmlfilename = HttpContext.Current.Server.MapPath("Precious\\" + fileName + ".txt");
var str = content;
var sw = StreamWriter.Null;
try
{
using (sw = new StreamWriter(htmlfilename, false, code))
{
sw.Write(str);
sw.Flush();
}
}
catch (IOException ioex)
{
throw new IOException(ioex.Message);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
sw.Close();
}
}
/// <summary>
/// 读文件
/// </summary>
/// <param name="filename">文件路径</param>
/// <param name="encoding">文件编码</param>
/// <returns></returns>
public static string Read_Txt(string filename, string encoding)
{
if (string.IsNullOrEmpty(filename))
{
throw new ArgumentNullException(filename);
}
if (string.IsNullOrEmpty(encoding))
{
throw new ArgumentNullException(encoding);
}
var code = Encoding.GetEncoding(encoding);
var temp = HttpContext.Current.Server.MapPath("Precious\\" + filename + ".txt");
var str = string.Empty;
if (!System.IO.File.Exists(temp)) return str;
var sr = StreamReader.Null;
try
{
using (sr = new StreamReader(temp, code))
{
str = sr.ReadToEnd();
}
}
catch (IOException ioex)
{
throw new IOException(ioex.Message);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
sr.Close();
}
return str;
}
/// <summary>
/// 拷贝文件
/// </summary>
/// <param name="orignFile">原始文件</param>
/// <param name="newFile">新文件路径</param>
public static void FileCoppy(string orignFile, string newFile)
{
if (string.IsNullOrEmpty(orignFile))
{
throw new ArgumentException(orignFile);
}
if (string.IsNullOrEmpty(newFile))
{
throw new ArgumentException(newFile);
}
System.IO.File.Copy(orignFile, newFile, true);
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="path">路径</param>
public static void FileDel(string path)
{
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException(path);
}
System.IO.File.Delete(path);
}
/// <summary>
/// 移动文件
/// </summary>
/// <param name="orignFile">原始路径</param>
/// <param name="newFile">新路径</param>
public static void FileMove(string orignFile, string newFile)
{
if (string.IsNullOrEmpty(orignFile))
{
throw new ArgumentException(orignFile);
}
if (string.IsNullOrEmpty(newFile))
{
throw new ArgumentException(newFile);
}
System.IO.File.Move(orignFile, newFile);
}
//创建路径
public static void CreatePath(string FilePath)
{
if (!Directory.Exists(FilePath))
{
Directory.CreateDirectory(FilePath);
}
}
//创建文件
public static void CreateFile(string FilePath)
{
if (!File.Exists(FilePath))
{
FileStream fs = File.Create(FilePath);
fs.Close();
}
}
}
Net文件递归查找并保存的更多相关文章
- linux递归查找文件内容并替换
sed -i 's/原字符串/替换后字符串/g' `grep '搜索关键字' -rl /data/目标目录/ --include "*.html"` 上面是递归查找目录中所有的HT ...
- [javaSE] IO流(递归查找指定文件)
递归方法,实现查找目录中以.java为后缀的文件路径,并存入文本文件中 定义一个静态方法fileToLine(),传入参数:File对象目录,List集合对象(List<File> 这样做 ...
- Java递归查找层级文件夹下特定内容的文件
递归查找文件 引言 或许是文件太多,想找某个文件又忘记放哪了;又或者是项目改造,需要将外部调用接口进行改造,项目太多,又无法排查.那么怎么快速找到自己想要的内容就是一件值得思考的事情了. 根据特定内容 ...
- 在文件夹中 的指定类型文件中 查找字符串(CodeBlocks+GCC编译,控制台程序,仅能在Windows上运行)
说明: 程序使用 io.h 中的 _findfirst 和 _findnext 函数遍历文件夹,故而程序只能在 Windows 下使用. 程序遍历当前文件夹,对其中的文件夹执行递归遍历.同时检查遍历到 ...
- Java 7 中 NIO.2 的使用——文件递归操作
众所周知,递归编程是一项有争议的技术,因为它需要大量的内存,但是它能简化一些编程任务.基本上,一个递归操作都是程序调用自己传递参数修改的值或者参数传递到当前的程序循环中.递归编程通常用来计算阶乘斐波那 ...
- 有关文件夹与文件的查找,删除等功能 在 os 模块中实现
最近在写的程序频繁地与文件操作打交道,这块比较弱,还好在百度上找到一篇不错的文章,这是原文传送门,我对原文稍做了些改动. 有关文件夹与文件的查找,删除等功能 在 os 模块中实现.使用时需先导入这个模 ...
- Shell实例----------从文件夹里面多个文件里面查找指定内容
脚本执行方式:脚本名称 目录的路径 要查找的内容 #!/bin/bash num=`ls $1 |tr ' ' '^$'|wc -l` for i in `seq 1 $num` do file_n ...
- JAVA之旅(二十九)——文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习
JAVA之旅(二十九)--文件递归,File结束练习,Properties,Properties存取配置文件,load,Properties的小练习 我们继续学习File 一.文件递归 我们可以来实现 ...
- find 递归/不递归 查找子目录的方法
1.递归查找(find 命令 是递归遍历文件夹的) 命令:find . -name “*.txt” //当前路径下递归查找以.txt结尾的文件夹 2.不递归查找 find . -name “*.txt ...
随机推荐
- mysql 获取单个科目的平均分
mysql> select * from test; +----+----------+-------+-----------+ | id | name | score | subject | ...
- mysql max()函数,min()函数,获取最大值以及最小值
mysql> select * from table1; +----------+------------+-----+---------------------+ | name_new | t ...
- OpenFOAM——90度T型管
本算例来自<ANSYS Fluid Dynamics Verification Manual>中的VMFL010: Laminar Flow in a 90° Tee-Junction. ...
- SOAP简介及实例
SOAP 是基于 XML 的简易协议,可使应用程序在 HTTP 之上进行信息交换. 或者更简单地说:SOAP 是用于访问网络服务的协议. 您应当具备的基础知识 在继续学习之前,您需要对下面的知识有基本 ...
- pgsql 聚合函数array_to_string,ARRAY_AGG
array_to_string--将sql中的数组转为字符串 ARRAY_AGG--将sql中的数据转为数组处理 以下给大家一个简单的例子即可体会: 1.需求 2.数据库中原数据 1.pn ...
- maven依赖 dependency中scope=compile 和 provided区别
问题再现 上次这边朋友问我一个问题,就是他们在pom.xml中的dependency中,看到有一些是provided的情况,比如如下: <dependency> <groupId&g ...
- NGINX Cache Management (.imh nginx)
In this article, we will explore the various NGINX cache configuration options, and tips on tweaking ...
- Anaconda3_5.3.1+Pycharm2018.3安装步骤
最近更新了Anaconda软件,重新配置了以下Python开发环境,结果之前旧环境开发的好好的程序竟然跑不起来.网上各种搜索,各种找答案还是没有一篇靠谱的文章教我把问题解决.走了各种弯路,足足整了几天 ...
- js object 添加键值
第一种方法let obj ={"name":"tom","age":16}let key = "id";let valu ...
- c++11 为什么使用ref,和引用的区别
std::ref只是尝试模拟引用传递,并不能真正变成引用,在非模板情况下,std::ref根本没法实现引用传递,只有模板自动推导类型时,ref能用包装类型reference_wrapper来代替原本会 ...