【发布时间】:2013-09-16 15:32:08
【问题描述】:
是否可以检查服务器上是否存在扩展名为 .pdf 的文件?我知道我可以使用: File.Exists(Server.MapPath("/somepath"))); 但这是我在寻找特定文件的时候。是否可以只查找文件扩展名?
【问题讨论】:
标签: c# asp.net pdf file-exists
是否可以检查服务器上是否存在扩展名为 .pdf 的文件?我知道我可以使用: File.Exists(Server.MapPath("/somepath"))); 但这是我在寻找特定文件的时候。是否可以只查找文件扩展名?
【问题讨论】:
标签: c# asp.net pdf file-exists
是的,您可以找到所有具有特定扩展名的文件,这是一个示例代码:
string[] files = System.IO.Directory.GetFiles(Server.MapPath("/somepath"), "*.txt");
希望这会有所帮助。
【讨论】:
目前我正在使用这种方法:
public IEnumerable<FileInfo> FindFilesInDirectory(string dirPath)
{
return Directory.Exists(dirPath)
? Directory.GetFiles(dirPath, "*.pdf", SearchOption.AllDirectories)
.ToList()
.ConvertAll(x => new FileInfo(x))
: null;
}
不确定它是否适用于Server.MapPath("/somepath")
【讨论】: