一、获取文件夹中指定名称文件的全路径(不包含文件后缀)
点击查看代码
/// <summary>
/// 获取指定名称文件的全路径
/// </summary>
/// <param name="dirPath">文件夹路径</param>
/// <param name="fileName">文件名称</param>
/// <returns>文件全路径</returns>
public static string GetSpecNameFilePath(string dirPath,string fileName)
{
string fileFullPath = "";
if (Directory.Exists(dirPath))
{
DirectoryInfo root = new DirectoryInfo(dirPath);
FileInfo[] files = root.GetFiles();
foreach (FileInfo fileInfo in files)
{
string name = fileInfo.Name.Split('.')[0];
if (name == fileName)
{
fileFullPath = fileInfo.FullName;
break;
}
}
}
else
{
return "不存在文件夹!";
}
return fileFullPath;
}
二、获取文件夹中指定后缀文件的全路径(只取第一个文件)
点击查看代码
/// <summary>
/// 获取指定格式文件的全路径
/// </summary>
/// <param name="dirPath">文件夹路径</param>
/// <param name="fileExten">文件后缀</param>
/// <returns>文件全路径</returns>
public static string GetSpecFormatFilePath(string dirPath, string fileExten)
{
string fileFullPath = "";
if (Directory.Exists(dirPath))
{
DirectoryInfo root = new DirectoryInfo(dirPath);
FileInfo[] files = root.GetFiles();
foreach (FileInfo fileInfo in files)
{
string extenStr = fileInfo.Extension;
if (string.Equals(extenStr,fileExten,StringComparison.CurrentCultureIgnoreCase))
{
fileFullPath = fileInfo.FullName;
break;
}
}
}
else
{
return "不存在文件夹!";
}
return fileFullPath;
}