您的位置:首页 > 编程语言 > C#

C#文件、文件夹操作

2009-08-25 22:26 288 查看
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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: