[c#]代码库
using System;
using System.Text;
using System.Web;
using System.IO;
public class FileHelper
{
#region 文件/目录是否存在
/// <summary>
/// 判断给定字符串是否是有效存在的目录
/// </summary>
/// <param name="path">目录字符串</param>
/// <returns></returns>
public static bool IsExistDirectory(string path)
{
return Directory.Exists(path);
}
/// <summary>
/// 判断给定字符串是事有效存在的文件
/// </summary>
/// <param name="path">文件路径</param>
/// <returns></returns>
public static bool IsExistFile(string path)
{
return File.Exists(path);
}
#endregion
#region 取得文件后缀名
/// <summary>
/// 获取指定文件的扩展名(不含.)
/// </summary>
/// <param name="file">文件路径</param>
/// <returns></returns>
public static string GetExtensionStr(string file)
{
string str = Path.GetExtension(file);
if (str.Length > 1)
return str.Substring(1);
else
return "";
}
#endregion
#region 取得文件名
/// <summary>
/// 取得文件名(包含扩展名)
/// </summary>
/// <param name="file">文件路径</param>
/// <returns></returns>
public static string GetFileFullName(string file)
{
return Path.GetFileName(file);
}
/// <summary>
/// 取得文件名(不含扩展名)
/// </summary>
/// <param name="file">文件路径</param>
/// <returns></returns>
public static string GetFileNameWithoutExtension(string file)
{
return Path.GetFileNameWithoutExtension(file);
}
/// <summary>
/// 取得指定路径(目录/文件)的最末级目录名
/// </summary>
/// <param name="path">路径</param>
/// <returns></returns>
public static string GetDirectoryName(string path)
{
if (File.Exists(path))
{
return Path.GetDirectoryName(path);
}
else // if (Directory.Exists(path))
{
return Path.GetDirectoryName(Path.Combine(path, Path.DirectorySeparatorChar.ToString()));
}
}
#endregion
#region 拷贝文件
/// <summary>
/// 拷贝文件
/// </summary>
/// <param name="sourceFile">原始文件</param>
/// <param name="destFile">新文件路径</param>
public static void FileCoppy(string sourceFile, string destFile)
{
File.Copy(sourceFile, destFile, true);
}
#endregion
#region 删除文件
/// <summary>
/// 删除文件
/// </summary>
/// <param name="file">路径</param>
public static void FileDelete(string file)
{
File.Delete(file);
}
#endregion
#region 移动文件
/// <summary>
/// 移动文件
/// </summary>
/// <param name="OrignFile">原始路径</param>
/// <param name="NewFile">新路径</param>
public static void FileMove(string OrignFile, string NewFile)
{
Directory.CreateDirectory(Path.GetDirectoryName(NewFile));
if (File.Exists(OrignFile))
File.Move(OrignFile, NewFile);
}
#endregion
#region 创建目录
/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="Path"></param>
public static void FolderCreate(string Path)
{
// 判断目标目录是否存在如果不存在则新建之
if (!Directory.Exists(Path))
Directory.CreateDirectory(Path);
}
#endregion
#region 删除目录及子目录及文件
/// <summary>
/// 递归删除文件夹目录及文件
/// </summary>
/// <param name="dirPath"></param>
/// <returns></returns>
public static void FolderDelete(string dirPath)
{
Directory.Delete(dirPath, true);
}
#endregion
#region 文件夹内容COPY
/// <summary>
/// 指定文件夹下面的所有内容copy到目标文件夹下面
/// </summary>
/// <param name="sourcePath">原始路径</param>
/// <param name="destPath">目标文件夹</param>
public static void FolderCopy(string sourcePath, string destPath)
{
string sPath = Path.GetFullPath(sourcePath);
string dPath = Path.GetFullPath(destPath);
foreach (string key in Directory.GetDirectories(sPath, "*", SearchOption.AllDirectories))
{
if (!Directory.Exists(key.Replace(sPath, dPath)))
Directory.CreateDirectory(key.Replace(sPath, dPath));
}
foreach (string file in Directory.GetFiles(sPath, "*", SearchOption.AllDirectories))
{
File.Copy(file, file.Replace(sPath, dPath), true);
}
}
#endregion
#region 获取文件大小
/// <summary>
/// 获取文件大小
/// </summary>
/// <param name="file">文件路径</param>
/// <returns></returns>
public static long GetFileSize(string file)
{
FileInfo fi = new FileInfo(file);
if (fi != null)
return fi.Length;
else
return 0;
}
#endregion
#region 获取文件夹大小
/// <summary>
/// 获取文件夹大小
/// </summary>
/// <param name="dirPath">文件夹路径</param>
/// <returns></returns>
public static long GetFolderSize(string dirPath)
{
if (!Directory.Exists(dirPath))
return 0;
long len = 0;
FileInfo fi;
foreach (string key in Directory.GetFiles(dirPath, "*", SearchOption.AllDirectories))
{
fi = new FileInfo(key);
len += fi.Length;
}
return len;
}
#endregion
}
by: 发表于:2017-12-20 17:30:17 顶(0) | 踩(0) 回复
??
回复评论