简体   繁体   English

如何检查服务器上是否存在pdf文件?

[英]How to check if any pdf file exists on server?

Is it possible to check if any file with .pdf extension exists on server? 是否可以检查服务器上是否存在任何扩展名为.pdf的文件? I know i can use: File.Exists(Server.MapPath("/somepath"))); 我知道我可以使用:File.Exists(Server.MapPath(“ / somepath”))); but this is when I'm looking for specific file. 但这是我在寻找特定文件的时候。 Is it possible to just look for the file extension? 是否可以仅查找文件扩展名?

Yes you can find all the files with certain extension, here is a sample code: 是的,您可以找到带有某些扩展名的所有文件,这是示例代码:

string[] files = System.IO.Directory.GetFiles(Server.MapPath("/somepath"), "*.txt");

Hope this helps. 希望这可以帮助。

Currently i'm working with this Method: 目前,我正在使用此方法:

public IEnumerable<FileInfo> FindFilesInDirectory(string dirPath)
{
    return Directory.Exists(dirPath)
               ? Directory.GetFiles(dirPath, "*.pdf", SearchOption.AllDirectories)
                          .ToList()
                          .ConvertAll(x => new FileInfo(x))
               : null;
}

Not sure if it works with Server.MapPath("/somepath") 不知道它是否与Server.MapPath("/somepath")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM