为什么文件的LastWriteTime和同一文件的存储日期和时间不相等,即使它们是相同的?

时间:2016-01-06 11:04:19

标签: vb.net

我正在尝试通知用户对文件进行了更改,方法是在应用程序设置中的日期不等于文件的LastWriteTime时使图片框变为灰色。但是,当我运行程序并且两者相等时(据我所知)它仍然表示它们不相等。我添加了一个文本框以便可视化,但我不知道我做错了什么......

我的代码:

 Private Sub Button11_Click(sender As System.Object, e As System.EventArgs) Handles Button11.Click
    Try
        Process.Start("G:\divi\RATH\Applicaties\RSM\Program\Settings\IGART\WervelSTX\log_wervelSTX.txt")
        PictureBox_wervelSTX.BackColor = Color.Maroon
        fsw_wervelSTX_file = My.Computer.FileSystem.GetFileInfo("G:\divi\RATH\Applicaties\RSM\Program\Settings\IGART\WervelSTX\log_wervelSTX.txt")
        MsgBox(fsw_wervelSTX_file.LastWriteTime, MsgBoxStyle.Information)
        My.Settings.fsw_wevelSTX_lastwrite = fsw_wervelSTX_file.LastWriteTime
    Catch ex As Exception
        MessageBox.Show("Error opening file: " & ex.Message)
    End Try
End Sub

链接中有一个MsgBox的屏幕截图: MsgBox

当用户点击按钮打开指定文件时,LastWriteTime存储到My.Settings:

代码:

<!-- Generate Summary -->
    <xsl:template name="GenerateSummary">
    <xsl:param name="Content"/>
    <xsl:param name="Length"/>
    <xsl:param name="Suffix"/>
    <xsl:variable name="cleanContent">
        <xsl:call-template name="RemoveHtml">
        <xsl:with-param name="String" select="$Content"/>
        </xsl:call-template>
    </xsl:variable>
    <xsl:call-template name="SubstringBeforeLast">
        <xsl:with-param name="String"
        select="substring($cleanContent, 1, $Length)"/>
        <xsl:with-param name="Char" select="' '"/>
    </xsl:call-template>
    <xsl:if test="string-length($cleanContent) &gt; $Length">
        <xsl:value-of select="$Suffix"
        disable-output-escaping="yes"/>
    </xsl:if>
    </xsl:template>

    <!-- RemoveHTML -->
    <xsl:template name="RemoveHtml">
      <xsl:param name="String"/>
      <xsl:choose>
        <xsl:when test="contains($String, '&lt;')">
          <xsl:value-of select="substring-before($String, '&lt;')"/>
          <xsl:call-template name="RemoveHtml">
            <xsl:with-param name="String"
              select="substring-after($String, '&gt;')"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:value-of select="$String"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>
    <xsl:template name="SubstringBeforeLast">
      <xsl:param name="String" />
      <xsl:param name="Char" />
      <xsl:param name="subsequent"/>
      <xsl:choose>
        <xsl:when test="contains($String, $Char)">
          <xsl:if test="$subsequent = 1">
            <xsl:value-of select="$Char"/>
          </xsl:if>
          <xsl:value-of select="substring-before($String, $Char)"/>
          <xsl:call-template name="SubstringBeforeLast">
            <xsl:with-param name="String"
              select="substring-after($String, $Char)" />
            <xsl:with-param name="Char" select="$Char" />
            <xsl:with-param name="subsequent" select="1"/>
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
          <xsl:if test="$subsequent != 1">
            <xsl:value-of select="$String"/>
          </xsl:if>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:template>

提前致谢。

2 个答案:

答案 0 :(得分:2)

从您发布的代码开始,我猜这是一个序列化的事情。日期时间可能存储在您的设置中,如2016-01-06 12:08:11(除了实际日期格式),而实际日期时间可能有更多/更好的“分辨率”,并包含2016-01-06 12:08:11.123之类的内容。我建议其中一个:

  • 以特定(指定)格式存储,并确保您的比较不超过实际存储的“分辨率”
  • 将值存储为刻度或其他特定长整数值(例如UNIX时间戳)
  • 比较时允许一些'保证金'

哪个最好取决于您和要求/用例。

文件日期时间all sortsWhy does the timestamp of a file increase by up to 2 seconds when I copy it to a USB thumb drive?个奇怪的东西,但我目前最好的猜测是你只是比较两个略微的不同值。

答案 1 :(得分:0)

添加到加载代码:

 Dim timecompare As Date

Private Sub IGART_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            fsw_wervelSTX_file = My.Computer.FileSystem.GetFileInfo("G:\divi\RATH\Applicaties\RSM\Program\Settings\IGART\WervelSTX\log_wervelSTX.txt")
    timecompare = fsw_wervelSTX_file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
    If Not My.Settings.fsw_wevelSTX_lastwrite = timecompare Then
        PictureBox_wervelSTX.BackColor = Color.Lime
        MsgBox("Time in My.Settings: " & My.Settings.fsw_wevelSTX_lastwrite & "  LastWriteTime: " & fsw_wervelSTX_file.LastWriteTime, MsgBoxStyle.Information)
    Else
        PictureBox_wervelSTX.BackColor = Color.Maroon
        MsgBox("It is the same", MsgBoxStyle.Information)
    End If

添加到按钮代码:

Dim time As Date
Private Sub Button11_Click(sender As System.Object, e As System.EventArgs) Handles Button11.Click
    Try
        Process.Start("G:\divi\RATH\Applicaties\RSM\Program\Settings\IGART\WervelSTX\log_wervelSTX.txt")
        PictureBox_wervelSTX.BackColor = Color.Maroon
        fsw_wervelSTX_file = My.Computer.FileSystem.GetFileInfo("G:\divi\RATH\Applicaties\RSM\Program\Settings\IGART\WervelSTX\log_wervelSTX.txt")
        MsgBox(fsw_wervelSTX_file.LastWriteTime, MsgBoxStyle.Information)
        My.Settings.fsw_wevelSTX_lastwrite = fsw_wervelSTX_file.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
    Catch ex As Exception
        MessageBox.Show("Error opening file: " & ex.Message)
    End Try
End Sub