C#文件及目录操作类汇总
来源:长沙北大青鸟|发布时间:2017-03-27|浏览量:
C# 文件操作类大全
1、读取文件
1.1.操作系统默认编码
//using
System.IO;
StreamReader s = File.OpenText(%%1);
string %%2 = null;
while ((%%2 = s.ReadLine()) != null){
%%3
}
s.Close();
1.2.UTF-8编码
/*
using
System.IO;
using System.Text;
*/
StreamReader srfile = new
StreamReader(%%1,Encoding.UTF8);
while ((String %%2 = srfile.ReadLine()) !=
null)
{
%%3
}
srfile.Close();
1.3.分块读取
/*
using
System.IO;
using System.Text;
*/
try
{
FileStream fs=new
FileStream(%%1,FileMode.Open,FileAccess.Read);
BinaryReader br=new
BinaryReader(fs,new ASCIIEncoding());
byte[]
chunk;
do
{
chunk=br.ReadBytes(10240);
if(chunk.Length>0)
{
%%2
//chunk,chunk.Length
}
}
while(chunk.Length>0);
fs.Close();
}
catch
{
//return
-1;
}
2.写入文件
//using System.IO;
FileInfo f = new
FileInfo(%%1);
StreamWriter w =
f.CreateText();
w.WriteLine(%%2);
w.Flush();
w.Close();
3.移动一个目录下所有文件到一个文件夹中
/*
using
System.IO;
using System.Collections;
*/
string path =
(%%2.LastIndexOf(Path.DirectorySeparatorChar) == %%2.Length - 1) ? %%2 :
%%2+Path.DirectorySeparatorChar;
string parent =
Path.GetDirectoryName(%%1);
Directory.CreateDirectory(path +
Path.GetFileName(%%1));
DirectoryInfo dir = new
DirectoryInfo((%%1.LastIndexOf(Path.DirectorySeparatorChar) == %%1.Length - 1) ?
%%1 : %%1 + Path.DirectorySeparatorChar);
FileSystemInfo[] fileArr =
dir.GetFileSystemInfos();
Queue Folders = new
Queue(dir.GetFileSystemInfos());
while
(Folders.Count>0)
{
FileSystemInfo tmp = Folders.Dequeue();
FileInfo
f = tmp as FileInfo;
if (f == null)
{
DirectoryInfo d = tmp as
DirectoryInfo;
foreach (FileSystemInfo fi in
d.GetFileSystemInfos())
{
Folders.Enqueue(fi);
}
}
else
{
f.MoveTo(path+f.Name);
}
}
4.文件RSA高级加密
/*
using
System.IO;
using System.Security.Cryptography;
private
RSACryptoServiceProvider _rsa;
*/
FileStream fin=new
FileStream(%%1,FileMode.Open,FileAccess.Read);
FileStream fout=new
FileStream(%%2,FileMode.OpenOrCreate,FileAccess.Write);
byte[] readBuffer=new
byte[128];
fin.Read(readBuffer,0,readBuffer.Length);
byte[]
encryptedBuffer=_rsa.Encrypt(readBuffer,true);
_rsa.Clear();
5.计算文件大小
/*
using
System.IO;
private const long KB=1024;
private const long
MB=1024*KB;
private const long GB=1024*MB;
*/
FileInfo fi = new
FileInfo(%%1);
long filesize= fi.Length;
string
showsize;
if(filesize>=GB)
showsize.format("%0.2f
GB",(double)filesize/GB);
else if(filesize>=MB)
showsize.format("%0.2f
MB",(double)filesize/MB);
else if(filesize>=KB)
showsize.format("%0.2f
KB",(double)filesize/KB);
else if(filesize>1)
showsize.format("%ld
Bytes",filesize);
else
showsize="1 Byte";
string
%%2=showsize;
6.计算文件夹的大小
/*
using System.IO;
private const
long KB=1024;
private const long MB=1024*KB;
private const long
GB=1024*MB;
*/
private static long FolderFileSize(string
path)
{
long size = 0;
try
{
FileInfo [] files = (new
DirectoryInfo(path)).GetFiles();
foreach(FileInfo file in files)
{
size
+= file.Length;
}
}
catch(Exception
ex)
{
MessageBox.Show(ex.Message);
}
return
size;
}
private static long FolderSize(string path)
{
long Fsize
= 0;
try
{
Fsize = FolderFileSize(path);
DirectoryInfo [] folders =
(new DirectoryInfo(path)).GetDirectories();
foreach(DirectoryInfo folder in
folders)
Fsize += FolderSize(folder.FullName);
}
catch(Exception
ex)
{
MessageBox.Show(ex.Message);
}
return Fsize;
}
long
filesize= FolderSize(%%1);
string
showsize;
if(filesize>=GB)
showsize.format("%0.2f
GB",(double)filesize/GB);
else if(filesize>=MB)
showsize.format("%0.2f
MB",(double)filesize/MB);
else if(filesize>=KB)
showsize.format("%0.2f
KB",(double)filesize/KB);
else if(filesize>1)
showsize.format("%ld
Bytes",filesize);
else
showsize="1 Byte";
string
%%2=showsize;
7.快速获得当前程序的驱动器、路径、文件名和扩展名
//using
System.IO;
DirectoryInfo dires= new
DirectoryInfo(Application.StartupPath);
string strQDQ = null;//驱动器
strQDQ
= dires.Root.ToString();
string strPath
=Application.ExecutablePath;//路径
string FName = null;//文件名
string
FNameExt = null;//扩展名
FileInfo FileIno = new FileInfo(strPath);
FName =
FileIno.Name;
FNameExt =
FileIno.Extension;
下一篇:如何用C#创建和读取XML文件
扫码关注微信公众号了解更多详情
跟技术大咖,专业导师一起交流学习
- 猜你喜欢
- >C#文件及目录操作类汇总
- 推荐阅读