C#文件、文件夹操作

简介:

using System; using System.Collections.Generic; using System.Linq; using System.Windows; using System.Text; using System.IO; //文件操作所属的命名空间。 namespace FileOperate { public class FileOperate//对文件的操作类 { //删除文件方法。 public bool DeletFile(string Filefullpath) { if (File.Exists(Filefullpath) == true) //判断该文件是否存在。 { File.SetAttributes(Filefullpath, FileAttributes.Normal);//将删除文件的属性设为一般属性。 File.Delete(Filefullpath); return true; } else { return false; } } //获取文件名方法。 public string GetFileName(string Filefullpath) //获取文件名。针对没有扩展名的情况。 { if (File.Exists(Filefullpath) == true) { FileInfo F = new FileInfo(Filefullpath); //利用FileInfo的.Name方法获取文件名。 return F.Name; } else { return null; } } public string GetFileName(string Filefullpath, bool IncludeExtension) //重载获取文件名方法。针对有扩展名的情况。 { if (File.Exists(Filefullpath) == true) { FileInfo F = new FileInfo(Filefullpath); if (IncludeExtension == true) { return F.Name; } else { return F.Name.Replace(Filefullpath, Filefullpath + "");//第一个参数为原文件名,后一个参数为替换后的文件名。 } } else { return null; } } //获取文件扩展名的方法。 public string GetFileExtension(string Filefullpath) //获取文件的扩展名。 { if (File.Exists(Filefullpath) == true) { FileInfo F = new FileInfo(Filefullpath); return F.Extension; } else { return null; } } //打开文件的方法。 public bool OpenFile(string Filefullpath) { if (File.Exists(Filefullpath)) { System.Diagnostics.Process.Start(Filefullpath); return true; } else { return false; } } //获取文件大小的方法。 public string GetFileSize(string Filefullpath) { if (File.Exists(Filefullpath) == true) { FileInfo F = new FileInfo(Filefullpath); long FL = F.Length; //注意:这里获取文件的大小要用到long型数据,因为F.Length为long型。 if (FL > 1024 * 1024 * 1024) { // KB MB GB return System.Convert.ToString(Math.Round((FL + 0.00) / (1024 * 1024 * 1024), 2))+"GB"; } else if (FL > 1024 * 1024) { return System.Convert.ToString(Math.Round((FL + 0.00) / (1024 * 1024), 2) )+ "MB"; } else { return System.Convert.ToString(Math.Round((FL + 0.00) / (1024 ), 2) )+ "KB"; } } else { return null; } } //将文件转换成二进制数。用来处理图片等文件时用。 public byte[] FileToStreamByte(string Filefullpath) { byte[] FileData=null; if (File.Exists(Filefullpath) == true) { FileStream FS = new FileStream(Filefullpath, FileMode.Open); FileData = new byte[FS.Length]; FS.Read(FileData, 0, FileData.Length);//参数1:读取的数组名,参数2:起始位置,参数3:读取长度。 FS.Close(); return FileData; } else { return null; } } //将二进制数据转换成文件。 public bool ByteStreamToFile(string CreateFilefullpath, byte[] SteamByte) { try { if (File.Exists(CreateFilefullpath) == true) { File.Delete(CreateFilefullpath); //如果该路径存在文件,删除该文件,该地址留给转换后的文件。 } FileStream FS; FS=File.Create(CreateFilefullpath); FS.Write(SteamByte, 0, SteamByte.Length); FS.Close(); return true; } catch { return false; } } //为了实现不同系统间的交互,要利用XML传递数据,传递XML文件必须对它进行加密, //进行加密即是:序列化XML文件 public bool SerializeXmlFile(string Filefullpath) { try { System.Data.DataSet DS = new System.Data.DataSet(); DS.ReadXml(Filefullpath); //将要序列化的数据读入DataSet数据集。 FileStream FS = new FileStream(Filefullpath + ".tmp", FileMode.OpenOrCreate); //要序列化一个文件,需调用的系统方法如下。 System.Runtime.Serialization.Formatters.Binary.BinaryFormatter FT = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); FT.Serialize(FS, DS);//参数1:图形要序列化成的流,参数2:要序列化的数据。 FS.Close(); DeletFile(Filefullpath); File.Move(Filefullpath + ".tmp", Filefullpath); return true; } catch { return false; } } //反序列化XML文件。 public bool DeserializeFile(string Filefullpath) { try { System.Data.DataSet DS = new System.Data.DataSet(); DS.ReadXml(Filefullpath); FileStream FS = new FileStream(Filefullpath, FileMode.Open); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter FT = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); ((System.Data.DataSet)FT.Deserialize(FS)).WriteXml(Filefullpath + ".tmp"); FS.Close(); DeletFile(Filefullpath); File.Move(Filefullpath + ".tmp", Filefullpath); return true; } catch { return false; } } } public class DirOperate //对文件夹的操作类。 { public enum OperateOption { ExistDelet, //存在,删除再创建。 ExistReturn//存在,直接返回。 } //创建文件夹。 public bool CreatDir(string DirFullPath,OperateOption DirOperateOption) { try { if (Directory.Exists(DirFullPath) == false) { Directory.CreateDirectory(DirFullPath); //如果文件夹不存在,直接创建文件夹。 } else if (DirOperateOption == OperateOption.ExistDelet) { Directory.Delete(DirFullPath, true); } return true; } catch { return false; } } //删除文件夹。 public bool DeletDir(string DirFullPath) { if (Directory.Exists(DirFullPath) == true) { Directory.Delete(DirFullPath); return true; } else { return false; } } //得到文件夹当前目录下的所有文件。 public string[] GetDir(string DirFullPath) { string[] FileList=null; if (Directory.Exists(DirFullPath) == true) { FileList=Directory.GetFiles(DirFullPath,"*.*",SearchOption.TopDirectoryOnly);//参数1:文件夹路径,参数2:要搜索的文件格式,参数3:指定搜索范围 (当前目录还是包含所有子目录); } return FileList; } //得到文件夹目录下的所有文件。 public string[] GetDir(string DirFullPath, SearchOption SO) { string[] FileList = null; if (Directory.Exists(DirFullPath) == true) { FileList = Directory.GetFiles(DirFullPath, "*.*", SO); } return FileList; } //搜索当前文件目录下的指定文件格式的文件。 public string[] GetDir(string DirFullPath, string SeacherPattern) { string[] FileList = null; if (Directory.Exists(DirFullPath) == true) { FileList = Directory.GetFiles(DirFullPath, SeacherPattern); } return FileList; } //搜索文件目录下的所有指定文件格式的文件。 public string[] GetDir(string DirFullPath, string SeacherPattern,SearchOption SO) { string[] FileList = null; if (Directory.Exists(DirFullPath) == true) { FileList = Directory.GetFiles(DirFullPath, SeacherPattern,SO); } return FileList; } }

目录
相关文章
|
1月前
|
XML C# 数据格式
使用C#操作XML文件
使用C#操作XML文件
11 0
|
1月前
|
C#
C# 文件操作(全部) 追加、拷贝、删除、移动文件、创建目录
C# 文件操作(全部) 追加、拷贝、删除、移动文件、创建目录
21 0
|
3月前
|
C#
C#读取html文件
C#读取html文件
28 3
|
3月前
|
C# 开发者
C# 10.0中的文件范围命名空间:简化代码组织的新方式
【1月更文挑战第18天】C# 10.0引入了文件范围的命名空间,这是一种新的语法糖,用于更简洁地组织和管理代码。文件范围命名空间允许开发者在每个文件的基础上定义命名空间,而无需显式使用花括号包裹整个文件内容。本文将深入探讨文件范围命名空间的工作原理、使用场景以及它们为C#开发者带来的便利。
|
5月前
|
C#
Visual Studio C# CS0006 C# 未能找到元数据文件
Visual Studio C# CS0006 C# 未能找到元数据文件
66 0
Visual Studio C# CS0006 C# 未能找到元数据文件
|
4天前
|
XML C# 数据格式
C# 解析XML文件
C# 解析XML文件
11 1
|
4月前
|
IDE C# 开发工具
C# | 多线程批量下载文件(创建N个线程同时批量下载文件,只需要几行代码而已)
批量下载文件时使用多线程可以有效缩短完成时间,本文将讲解如何使用C#+CodePlus扩展库快速完成多线程的文件下载。 大部分代码由IDE自动生成,需要我们自己编写的代码正好**10行**。也就是说,只需要10分钟,就可以手撸一个多线程的批量下载器。
85 0
C# | 多线程批量下载文件(创建N个线程同时批量下载文件,只需要几行代码而已)
|
29天前
|
安全 数据处理 C#
C# Post数据或文件到指定的服务器进行接收
C# Post数据或文件到指定的服务器进行接收
|
29天前
|
C# 开发工具 数据安全/隐私保护
C#实现基于Word保护性模板文件的修改
C#实现基于Word保护性模板文件的修改
|
2月前
|
C#
C# Winform 选择文件夹和选择文件
C# Winform 选择文件夹和选择文件
43 0