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 ...
随机推荐
- VS2017 Asp.Net调式闪退处理
- 去除空格JS
$(document).ready(function (){//通用方法去输入框前后空格 $("form").on("change",function () { ...
- lnmp环境脚本快速搭建
进入lnmp官网 https://lnmp.org/download.html 如图: 进入Linux服务器并执行wget命令下载 wget http://soft.vpser.net/lnmp/ln ...
- github上如何删除一个项目(仓库)
备忘 链接:https://blog.csdn.net/deng0zhaotai/article/details/38535251
- 【转】vue中样式被覆盖的问题,vue中的style不生效
转载:http://www.cnblogs.com/shangjun6/p/11416054.html 在我们引入外部的样式时,发现自己无论如何都改不了外部的样式,自己的样式老被覆盖,究其原因还是我们 ...
- JdkDynamicAopProxy与CglibAopProxy介绍
继续上一篇的介绍 1.上一篇分析到createAopProxy方法,创建Aop代理对象 protected final synchronized AopProxy createAopProxy() { ...
- VBA 如何检测一个中文字符串是否包含在另一个字符串中
Sub test() aaa = "江苏省南京市建邺区水西门大街34号" If InStr(aaa, Then MsgBox "在里面" Else MsgBox ...
- Python之schedule用法,类似linux下的crontab
# -*- coding: utf-8 -*- # author:baoshan import schedule import time def job(): print("I'm work ...
- LeetCode_415. Add Strings
415. Add Strings Easy Given two non-negative integers num1 and num2 represented as string, return th ...
- 【linux学习笔记八】常用命令
查询与自动挂载 #查询系统中已经挂载的设备 mount #依据配置文件/etc/fstab的内容,自动挂载 mount -a 挂载命令格式 mount [-t 文件系统] [-o 特殊选项] 设备文件 ...