遍历文件夹下的子文件夹及文件,将文件压缩处理
void Compression()
{
Info = "正在处理中。。。";
DirectoryInfo folder = new DirectoryInfo("c:\\temp");
try
{
FileSystemInfo[] files = folder.GetFileSystemInfos();
for (int i = 0; i < files.Length; i++)
{
Compression(files[i]);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
Info = "压缩完毕。";
}
void Compression(FileSystemInfo finfo)
{
if (!finfo.Exists) return;
DirectoryInfo dir = finfo as DirectoryInfo;
if (dir != null)
{
FileSystemInfo[] files = null;
try
{
files = dir.GetFileSystemInfos();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
if (file != null)
{//是文件
Info = String.Format("处理 {0}", file.FullName);
try
{
//压缩文件。。。
}
catch
{
logger.Error(String.Format("处理{0}失败", file.FullName));
}
}
else
{
Compression(files[i]);
}
}
}
else
{
FileInfo file = finfo as FileInfo;
Info = String.Format("处理 {0}", file.FullName);
try
{
//压缩文件。。。
}
catch
{
logger.Error(String.Format("处理{0}失败", file.FullName));
}
}
}