【问题标题】:How to check if any pdf file exists on server?如何检查服务器上是否存在任何pdf文件?
【发布时间】:2013-09-16 15:32:08
【问题描述】:

是否可以检查服务器上是否存在扩展名为 .pdf 的文件?我知道我可以使用: File.Exists(Server.MapPath("/somepath"))); 但这是我在寻找特定文件的时候。是否可以只查找文件扩展名?

【问题讨论】:

    标签: c# asp.net pdf file-exists


    【解决方案1】:

    是的,您可以找到所有具有特定扩展名的文件,这是一个示例代码:

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

    希望这会有所帮助。

    【讨论】:

    • 是的,就是这样。那么你能告诉我是否要删除所有这些文件(如果它们存在),我该如何管理?感谢您的解决方案
    • 嘿,没关系,我用 foreach 函数解决了这个问题。再次感谢!
    【解决方案2】:

    目前我正在使用这种方法:

    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")

    【讨论】:

      猜你喜欢
      • 2012-10-01
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      • 2016-02-29
      • 1970-01-01
      • 2010-12-12
      • 2012-05-15
      相关资源
      最近更新 更多