如何从中获取最新的修改日期的pdf文件并在VB上显示?

时间:2019-07-17 13:11:04

标签: vb.net

我正试图让VB读取特定文件夹中的最新修改的pdf文件,并在我的vb表单上显示pdf文件。我只能在我的vb表单上创建一个简单的pdf显示,而我对此感到困惑。有人可以帮忙吗?

无法找到解决我问题的方法。

    Dim testFile As System.IO.FileInfo
    Dim fileName As String
    Dim folderPath As String
    Dim fullPath As String

    testFile = My.Computer.FileSystem.GetFileInfo("C:\Users\example.pdf")
    folderPath = testFile.DirectoryName
    fileName = testFile.Name
    fullPath = My.Computer.FileSystem.CombinePath(folderPath, fileName)
    AxAcroPDF1.src = fullPath

我的vb表单应显示基于最新修改文件的PDF。

2 个答案:

答案 0 :(得分:2)

您可以使用IO.DirectoryInfo类来获取目录中的每个IO.FileInfo,同时专门针对PDF文件,然后使用LINQ通过LastWriteTime对它们进行排序,然后从集合中获取最后一个文件:

Dim folder As IO.DirectoryInfo = New IO.DirectoryInfo("my folder path here")
Dim lastModifiedPdf As IO.FileInfo = folder.GetFiles("*.pdf").OrderBy(Function(f) f.LastWriteTime).LastOrDefault()

If lastModifiedPdf IsNot Nothing Then
    '....
End If

答案 1 :(得分:0)

您需要调用两组函数来实现此目的。

A。 Directory.GetFiles-这将列出目录中的所有文件,并且具有提供搜索模式并查看子文件夹的选项。

B。 File.GetLastWriteTime-这将返回您传递给它的文件的上次修改时间。

您可以将这些功能放在一起,例如:

    <?php
    function testingZeros($a,$b,$aa,$bb){
      if($a = 0 || $b = 0 || $aa = 0 || $bb = 0)
       $answer = "Please enter real values execpt 0";
      return $answer;
    }

    if(isset($_POST['calculate'])){
    $a1 = ((isset($_POST['a1']))?$_POST['a1']:'');
    $b1 = ((isset($_POST['b1']))?$_POST['b1']:'');
    $c1 = ((isset($_POST['c1']))?$_POST['c1']:'');
    $a2 = ((isset($_POST['a2']))?$_POST['a2']:'');
    $b2 = ((isset($_POST['b2']))?$_POST['b2']:'');
    $c2 = ((isset($_POST['c2']))?$_POST['c2']:'');

    if(!empty($_POST['a1']) && !empty($_POST['b1']) && !empty($_POST['c1']) && !empty($_POST['a2']) && !empty($_POST['b2']) && !empty($_POST['c2'])){
     $_POST['message'] = testingZeros($_POST['a1'],$_POST['b1'],$_POST['a2'],$_POST['b2']);
    }
    }

    <form action="<?=$_SERVER['PHP_SELF']; ?>" method="POST">
    <b><em>a</em><sub>1</sub>:</b>
        <input type="text" value="<?php if(isset($_POST['a1'])){ echo $_POST['a1'];}?>" name="a1">
<b><em>b</em><sub>1</sub>:</b>
<input type="text" value="<?php if(isset($_POST['b1'])){ echo $_POST['b1'];}?>" name="b1">
<b><em>c</em><sub>1</sub>:</b>
    <input type="text" value="<?php if(isset($_POST['c1'])){ echo $_POST['c1'];}?>" name="c1">
    <b><em>a</em><sub>2</sub>:</b>
    <input type="text" value="<?php if(isset($_POST['a2'])){ echo $_POST['a2'];}?>" name="a2">
    <b><em>b</em><sub>2</sub>:</b>
   <input type="text" value="<?php if(isset($_POST['b2'])){ echo $_POST['b2'];}?>" name="b2">
<b><em>c</em><sub>2</sub>:</b>
<input type="text" value="<?php if(isset($_POST['c2'])){ echo $_POST['c2'];}?>" name="c2">

<button type="submit" name="calculate">Calculate</button>
    <b>Message:</b>
        <input type="text" value="<?php if(isset($_POST['message'])){ echo $_POST['message'];}?>" name="messsage" readonly>
    </form>

并最终使用以下命令调用此函数:

Private Function GetLatestModifiedFileName(searchFolder As String) As String
    Dim retVal = "<empty>"

    Dim filesInDirectory() = Directory.GetFiles(searchFolder)

    Dim latestModifiedtime As DateTime = DateTime.MinValue

    For Each fileInDirectory As String In filesInDirectory
        Dim currentFileModifiedTime As DateTime = File.GetLastWriteTime(fileInDirectory)

        If (currentFileModifiedTime > latestModifiedtime) Then
            retVal = fileInDirectory
            latestModifiedtime = currentFileModifiedTime
        End If
    Next

    Debug.Print("File: '{0}' was last modified on: '{1}'", retVal, latestModifiedtime)

    Return retVal
End Function

变量Dim lastModifiedFileName = GetLatestModifiedFileName("D:\Documents\") 将包含具有最新修改日期/时间的文件的完整路径。