C#对文件的各种操作方法


-
使用C#语言对文件的各种操作,不一定是最全的,但希望有你所需要的。
10.读取文件属性 FileInfo f= new FileInfo(path): //f Creation Time, f. FullName if(.Attributes FileAttributes ReadOnly)=0) %%2 elset %%3 11.写入属性 FileInfo f= new FileInfo(path); ∥设置只读 f Attributes my File Attributes I FileAttributes. Readonly: ∥设置可写 f Attributes my File Attributes&File Attributes. Readonly 12枚举一个文件夹中的所有文件夹 foreach(string %%2 in Directory. GetDirectories(path)) %%3 /DirectoryInfo dir new DirectoryInfo(path) Filelnfol files= dir. GetFiles(" "" foreach(Filelnfo %%2 in files)(%%3*/ 13复制文件夹 using System IO; using System. Collections string path =(%%2.LastIndexof(l)==%%2.Length-1)? %%02: %%2+1; string parent= Path. GetDirectory Name(path) Directory. CreateDirectory(path Path GetFileName(path)) DirectoryInfo dir new DirectoryInfo((path. LastIndexof(1)==path Length-1)? path: path +1") File SystemInfo fileArr= dir. Get File SystemInfos( Queue<File SystemInfo> Folders= new Queue<File SystemInfo>(dir. GetFile SystemInfos() while( Folders. Count>0) File SystemInfo tmp= Folders. Dequeue() FileInfo f= tmp as FileInfo (f== null) Directorylnfo d= tmp as Directorylnfo Directory. Create Directory(d. FullName Replace((parent LastIndexOf("")== parent Length-1)? parent: parent +"l, path)); foreach( File SystemInfo fi in d. GetFile SystemInfos() Folders. Enqueue(fi) else f Copy To(f. FulIName Replace(parent, path)): 14复制目录下所有的文件夹到另一个文件夹下 using System. IO; using System. Collections: DirectoryInfo d= new DirectoryInfo(path) foreach(Directorylnfo dirs in d. GetDirectories() Queue<File SystemInfo> al new Queue<File SystemInfo>(dirs. GetFile SystemInfos(o) while(al. Count >0) File SystemInfo temp= al Dequeue FileInfo file temp as FileInfo if (file== null) DirectoryInfo directory temp as DirectoryInfo Directory. Create Directory(path+ directory Name): foreach(File SystemInfo fsi in directory. GetFileSystemInfos( al.Enqueue(fsi) else File Copy(file. FullName, path file Name); 15移动文件夹 using System. Io; using System. Collections string filename Path GetFileName(path); string path=(%%2. LastIndexOf(\)==%%2. Length-1)? %%2: %%2+N if(Path GetPath Root(path)== Path GetPath Root(%%2)) Directory. Move(path, path + filename) else string parent= Path. GetDirectory Name(path) Directory. Create Directory(path Path GetFileName(path)) DirectoryInfo dir new DirectoryInfo(path LastIndexOf("1")== path Length-1)? path: path+1") File SystemInfo fileArr= dir. Get File SystemInfos( Queue<File SystemInfo> Folders new Queue<File SystemInfo>(dir. GetFile SystemInfosO while(Folders. Count >0) File SystemInfo tmp= Folders. Dequeue 0; FileInfo f= tmp as FileInfo (f = null) Directorylnfo d= tmp as Directorylnfo: DirectoryInfo dpath new DirectoryInfo(d. FullName Replace((parent LastindexOf(\")== parent Length 1)? parent: parent 11, path)); dpath Create() foreach(File SystemInfo fi in d. GetFile SystemInfos( Folders. Enqueue(fi); else f Move To(f. FullName Replaceparent, path) Directory Delete(path, true) 16移动日录下所有的文件夹到另一个目录下 using System. IO; using System. Collections; tring filename= Path GetF Name(path); if(Path GetPath Root(path)== Path GetPath Root(%%2)) foreach(string dir in Directory. Get Directories(path)) Directory. Move(dir, Path. Combine(%%2, filename)); else foreach(string dir2 in Directory. GetDirectories(path) string parent= Path. GetDirectoryName(dir2): Directory. Create Directory(Path. Combine(%%2, Path. GetFileName( dir2) string dir=(dir2. LastIndexof(11)== dir2. Length-1)? dir2: dir2+"l Directorylnfo dirdir new DirectoryInfo(dir) File SystemInfo fileArr dirdir GetFile SystemInfos(; QueueFile SystemInfo> Folders= new Queue<File SystemInfo>(dirdir. GetFile SystemInfos()) while( Folders. Count >0) FileSystemInfo tmp= Folders. Dequeue(: FileInfo f= tmp as FileInfo if(f== null) Directorylnfo d= tmp as Directorylnfo: DirectoryInfo dpath new DirectoryInfo(d. FullName Replace((parent LastIndexOf(1t)== parent Length 1)? parent: parent +",%%2)) dpath Create O foreach( File SystemInfo fi in d. GetFile SystemInfos( Folders. Enqueue(fi) else f Move To(f. FullName Replace(parent, %%2)) dirdir Delete(true) 17以一个文件夹的框架在另一个目录创建文件夹和空文件 using System. IO; using System. Collections bool b=false string path=(%%2.Lastindexof("")==%%2.Length-1)?%%2: %%2+"I string parent= Path. GetDirectoryName(path) Directory. Create Directory(path Path GetFileName(path)); Directorylnfo dir new DirectoryInfo((path LastIndexOf(1t)==path Length-1)? path: path+W"); File SystemInfo file Arr=dir. Get File SystemInfos( QueueFile SystemInfo> Folders= new Queue<File SystemInfo>(dir. GetFile SystemInfoso): while(Folders. Count >0) File SystemInfo tmp= Folders. Dequeue(; FileInfo f= tmp as FileInfo (f if(f==nu Directorylnfo d=tmp as DirectoryInfo; Directory. Create Directory(d. FullName Replace((parent LastIndexof(\")== parent Length-1)? parent parent +l, path)) foreach(File SystemInfo fi in d. GetFileSystemInfos() Folders. Enqueue(fi); else if(b) File. Create(f. FullName Replace(parent, path)); 18复制文件 File Copy(path, %%2); 19.复制一个文件夹下所有的文件到另一个目录 foreach(string file Str in Directory. GetFiles(path) File Copy((path LastIndexof(1)== path Length -1)? path + Path. GetFileName(file Str): path ""+Path. GetFileName (fileStr) (%%2 Lastindexof("1")==%%2.Length-1)?%%2 +Path. GetFileName (file Str): %%2+"I"+Path. GetFileName(file Str)) 20.提取扩展名 string %%2=Path. GetExtension(path) 21提取文件名 string %%2=Path. GetFileName(path) 22提取文件路径 string %%2=Path. GetDirectory Name(path) 23替换扩展名 File. Change Extension(path, %%2 24追加路径 string %%3=Path. Combine(path, %%2): 25移动文件 File Move(path, %%2+"\+file getname(path) 26移动一个文件夹下所有文件到另一个目录 foreach(string file Str in Directory. GetFiles(path) File Move((path LastIndexof("l)== path Length-1)? path +Path. GetFileName(file Str): path ""+Path. GetFileName(file Str), (%%2 LastIndexof("\")==%%2.Length-1)?%%2 +Path. GetFileName (file Str): %%2+"ll+Path. GetFileName(file Str) 27指定目录下搜索文件 using System. Text: using System. IO; string fileName=path; string dirName=%%2: Directorylnfo dirc=new DirectoryInfo(dirName); foreach(FileInfo file in dirc GetFiles() if(file Name. Index Of(fileName)>-1) return file. FullName: foreach(DirectoryInfo dir in dirc. GetDirectories()( return GetFile(fileName, dir. FullName) return"找不到指定的文件"; 28打开对话框 Open File Dialog pen File Dialog=new Open File Dialog() open File Dialog. Initial Directory="C:N;∥/注意这里写路径时要用c:而不是c openFileDialog Filter=文本文件*C#文件”cs所有文件"; open File Dialog. Restore Directory=true open FileDialog. FilterIndex=1; if (openFile Dialog Show Dialog()==Dialog Result. OK)I fName=openFileDialog FileName: File fileOpen=new File(fName) isFileHaveName=true path=fileOpen ReadFile( ath. Append Text("\); 29文件分割 File Stream fsr= new FileStream(path, File Mode. Open, File Access. Read); byte[ btArr= new byte[fsr Length fsr. Read(btArr, 0, btArr. Length) fsr. Close ( string strFileName=path Substring(path LastIndexof(l)+1) File Stream fsw= new File Stream(%%2+strFileName +1",FileMode Create, FileAccess. Write) fsw Write(btArr, 0, btArr Length/2)

-
2013-03-14
72KB
C# 对文件操作的方法(读取,删除,批量拷贝,删除
2013-09-25c# 对文件操作的方法(读取,删除,批量拷贝,删除 c# 对文件操作的方法(读取,删除,批量拷贝,删除 c# 对文件操作的方法(读取,删除,批量拷贝,删除
53KB
关于C#操作文件并在窗体程序的控件中显示文件的方法
2018-12-04关于C#操作文件并在窗体程序的控件中显示文件的方法,希望对大家有益
123KB
C#文本文件读取和写入(文件、目录操作)
2018-12-04基于C#语言(WindowForm窗体应用程序示例)实现文本文件读取和写入方法;文件的删除、移动和复制;对文件中的目录文件进行读取显示与删除等操作
47KB
C#des对文件进行加密解密
2016-05-02开发环境VS2013;编程语言C#;用des对称加解密方法对任意文本文件进行加解密操作。可通过选择读入文本文件的方式,对文件进行操作。程序已编译运行通过,可放心使用。
25KB
C#文件和目录操作类
2018-04-16C#文件和目录操作类 主要包含文件操作类和目录操作类两个类方法
95KB
C#对数据库操作类包括多种操作数据库的方法
2008-09-23C#对数据库操作的类,包括各种操作数据库的方法,用存储过程的方法,用查询语句的方法,都有。。简单调用就可以。。绝对方便!
1KB
C# 读取ini文件的操作帮助类, INIHelper.cs
2018-08-17C# 读取ini文件的操作帮助类, INIHelper.cs 封装了读写ini文件节点的方法,还有删除和判断是否存在某节点的方法 ini读取 ini写
68KB
使用C#操作PDF的方法
2011-04-09使用C#打开PDF,阅读,修改,保存 openFile.Filter = "PDF文件|*.pdf"; openFile.ShowDialog(); axAcroPDF1.setShowToolbar
111KB
C#用Sql数据库查询方法操作Excel表格
2018-01-11C#用Sql数据库查询方法操作Excel表格,用数据库语句操作Excel表格
53KB
C#文件的读写操作 FileStream类和StreamReader类
2010-10-16详细讲解c#中的文件的一些操作,例如FileStream类构造函数种类,以及方法,属性等。对窗口和控制台都有相当大的帮助。
339KB
C# 操作redis缓存数据库 dll文件及一些方法
2011-07-30winform开发中遇到操作redis的方法和文章总结下供感兴趣的用户参考之用并上传C#操作redis的dll文件
136KB
C#文件操作(包含保存、字体设置、浏览路径)
2017-09-10资源包括3个Demo实例,分别是:保存文件(SaveFileDialog控件)、字体设置(FontDialog控件)和浏览路径(FolderBrowserDialog控件)。代码展示使用方法,分享出来
30KB
C# 配置文件概述
2015-05-12配置文件概述: 应用程序配置文件是标准的XML 文件 XML 标记和属性是区分大小写的 它是可以 按需要更改的 开发人员可以使用配置文件来更改设置 而不必重编译应用程序 配置 文件的根节点是confi
7KB
C# 对rar文件操作,支持路径含有空格
2013-04-28内有两套对rar操作的方法, 通过调用本地rar.exe的支持对路径中含有空格的rar进行操作
58KB
C#读取XML文件并写入数据库
2012-12-31C#读取XML文件并写入数据库的三种方法,直接写入和先读取到内存再写入数据库
47KB
c#读取自定义配置文件
2016-11-15在开发Asp.net站点的时候,我们会遇到很多的配置参数:网站名称,上传图片后缀,上传文件后缀,关键字过滤,数据库连接字串等等,这些内容如果比较少的话,直接配置到Web.config文件中,借由.NE
97KB
C# MySQL 数据库操作类 (包含MySql.Data.Dll文件) 常用方法二三十多个
2009-12-26C# MySQL 数据库操作类 (包含MySql.Data.Dll文件) 常用方法二三十多个 最近项目中用到的就写了个
40KB
C#.net 使用json作为配置文件,解悉JSON示例
2019-01-22C#.net 使用json作为配置文件,解悉JSON示例 演示如何解悉JSON,此方法可用于使用一个JSON文件作为程序的配置文件来永久保存配置数据。比txt的更好用,配置文件也更易懂
7KB
C#快速读写EXCEL文件
2009-02-20C#将DATASET、DATATABLE数据汇出EXCEL,从EXCEL汇入DATASET、DATATABLE,采用线程操作中途可以取消。而且速度非常快,比Cells直接赋值的方法不知快了多少倍。
22KB
C#.NET 下使用COM组件操作Excel文件示例,入门参考
2012-08-28/*该程序使用VS2005编写,示例可运行通过,包含了使用C#中COM组件操作Excel的基本方法,供该方面的初学者参考 *运行前需要对com组件Microsoft Excel 12.0 Object
61KB
C# 引用不同文件夹下的Dll文件
2012-03-08当我们的程序需要引用多个dll,太多的dll引起混乱时,我们是不是可以将dll分类在不同的目录呢?如何引用不同文件夹的Dll文件,将是我们的问题?
264KB
遍历读取文件夹中的所有文件 与读取所有子目录 C#.Net实例代码
2013-03-18遍历读取文件夹中的所有文件 与读取所有子目录 C#.Net实例代码下载 这篇文章主要介绍了C#遍历文件夹及其子目录的方法,涉及C#文件与目录的基本操作技巧,简单实用,具有一定参考借鉴价值,需要的朋友可
1KB
C#版日志操作类,功能强大方便实用
2019-08-03C#版日志操作类,能够自定义日志文件类型,将业务日志、数据日志、异常日志等各类型日志分开记录,自动以日期和日志类型创建日志文本文件,开发时调用 WriteLog 一个方法即可完成日志记录,支持自定义日
5KB
C#调用系统功能解压zip文件
2018-12-22C#调用系统功能(zipfldr)解压zip文件,支持Windows XP、Windows Vista/7/8/10。其中提供了两种方法,一种只能支持编译计算机使用的操作系统,另一种支持各版本Wind
19.23MB
C#启动winscp实现SFTP客户端
2019-05-30C#直接调用winscp.exe(安装winscp后,在安装目录拷贝该目录下的所有文件到C#可执行程序目录)打开winscp,然后自动连上SFTP服务器,这样就可以直接使用winscp来进行操作了,比
81KB
C#登录,可实现数据库、txt文件、xml文件数据的增删改查
2018-12-26包括三个类四个窗口,登录(查)、注册(增)、注销(删)、改密(改),四个操作分别一个窗口,方法简单所以注释不是很详细,使用数据库记得改连接字符串哦!
15KB
C# ini文件操作测试程序
2012-12-12ini文件操作测试程序 本人写。。代码有注释 编程语言:C# 开发工具:VS2005 1)已封装成DLL供调用。 2)提供两个方法IniReadValue + IniWriteValue 不多说。。双
5KB
net c# 读写XML 操作
2010-05-27net c# 读写xml 的实例, 有两种写方式。以及对XML的修改,删除操作
8KB
C# 操作MySQL数据库类
2010-04-02C# 操作MySQL数据库类 非存储过程操作类,如果要用到存储过程的话,自已修改里面的代码,里面的存储过程被我注释了。 至于调用方法,应该不用我说下载者应该也懂得吧,如果是对C#一巧不通的初学者,不懂
22KB
C#拷贝文件简单实现方法
2020-09-03主要介绍了C#拷贝文件简单实现方法,主要分析了FileInfo类中CopyTo方法针对文件复制的操作技巧,非常简单实用,需要的朋友可以参考下
-
博客
python爬虫框架——scrapy(2) 实战练习
python爬虫框架——scrapy(2) 实战练习
-
博客
Docker安装tomcat出现404问题
Docker安装tomcat出现404问题
-
学院
【数据分析-随到随学】Hadoop数据分析
【数据分析-随到随学】Hadoop数据分析
-
学院
android笔试面试和实战课程
android笔试面试和实战课程
-
博客
最长公共子序列(LCS)
最长公共子序列(LCS)
-
下载
反应过程vghtet.zip
反应过程vghtet.zip
-
学院
仿真钢琴-javascript实战
仿真钢琴-javascript实战
-
学院
Metabase从入门到精通视频教程
Metabase从入门到精通视频教程
-
下载
C语言经典算法大全.pdf
C语言经典算法大全.pdf
-
下载
在线视频学习网站的设计与实现.
在线视频学习网站的设计与实现.
-
下载
VB6.0中编辑MSHFlexGrid复选行和列.txt
VB6.0中编辑MSHFlexGrid复选行和列.txt
-
学院
Qt and Qt Charts
Qt and Qt Charts
-
博客
关于投资银行和咨询的理解和感悟
关于投资银行和咨询的理解和感悟
-
下载
oxford 102flowers.zip
oxford 102flowers.zip
-
下载
SSH (Struts2+Spring3+Hibernate3) +Proxool_亲测成功 费大劲了.zip
SSH (Struts2+Spring3+Hibernate3) +Proxool_亲测成功 费大劲了.zip
-
学院
21年新消息队列RabbitMQ视频教程AMQP教程
21年新消息队列RabbitMQ视频教程AMQP教程
-
博客
oss-browser编译的时候,总是提示各种错误
oss-browser编译的时候,总是提示各种错误
-
学院
UE4游戏逆向与安全+FPS游戏逆向与安全
UE4游戏逆向与安全+FPS游戏逆向与安全
-
学院
UnitySocket异步聊天室
UnitySocket异步聊天室
-
学院
Unity游戏开发之数字华容道
Unity游戏开发之数字华容道
-
学院
商业的本质——杰克·韦尔奇著
商业的本质——杰克·韦尔奇著
-
学院
计算机网络基础
计算机网络基础
-
博客
日期类
日期类
-
博客
fcitx5在vscode中的BUG
fcitx5在vscode中的BUG
-
博客
OpenCV 基础
OpenCV 基础
-
博客
Redis知识点总结
Redis知识点总结
-
学院
微信支付2021系列之付款码支付一学就会java版
微信支付2021系列之付款码支付一学就会java版
-
下载
汽车CAN总线系统原理、设计与应用.rar
汽车CAN总线系统原理、设计与应用.rar
-
下载
shotcut-win64-201128
shotcut-win64-201128
-
博客
2021-01-24区间DP
2021-01-24区间DP