创建后“x”秒后删除文件

时间:2015-06-17 17:51:54

标签: vb.net function

我想在创建该文件15秒后删除文件。 我使用此代码但没有成功。

Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click
Dim test = Application.StartupPath & "\" + tte4
Timer1.Enabled = True
        If Timer1.Interval = 0 Then
            My.Computer.FileSystem.DeleteFile(test)
        End If
End Sub

3 个答案:

答案 0 :(得分:0)

您需要定义一个方法,当间隔过去时计时器会触发。

' Timer set to fire every 15 seconds
Dim Timer1 As New Timer(15000)
' OnTimedEvent is the method that will fire after 15 seconds
AddHandler Timer1.Elapsed, AddressOf OnTimedEvent
' Start the Timer
Timer1.Start()

OnTimedEvent如下:

Private Sub OnTimedEvent(source As Object, e As ElapsedEventArgs)
    ' Stop the timer from firing again
    Timer1.Stop()

    ' Delete your file
End Sub

参考

https://msdn.microsoft.com/en-us/library/k0wdddfy(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1

答案 1 :(得分:0)

您必须在Timer的Tick事件处理程序中处理定时事件(如果使用System.Timers.Timer,则必须处理):

Private m_strTest As String = String.Empty

Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click

    m_strTest = Application.StartupPath & "\" + tte4
    Timer1.Enabled = True

End Sub

如果使用System.Forms.Timer(最有可能):

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    System.IO.File.Delete(m_strTest)    
    Timer1.Enabled = False

End Sub

如果使用System.Timers.Timer:

Private Sub Timer1_Elapsed(ByVal source As Object, ByVal e As ElapsedEventArgs) Handles Timer1_Elapsed

    System.IO.File.Delete(m_strTest)
    Timer1.Stop()

End Sub

如果您从工具箱中添加了计时器,则上述代码将起作用(由于WithEvents)。如果以编程方式添加它,您还必须连接事件处理程序并从Timer1_Tick方法中删除句柄:

Private Sub Form1_Click(sender As Object, e As EventArgs) Handles Me.Click

    m_strTest = Application.StartupPath & "\" + tte4

    AddHandler Timer1.Tick, AddressOf Timer1_Tick

    Timer1.Enabled = True

End Sub

Private Sub Timer1_Tick(sender As Object, e As EventArgs)

    System.IO.File.Delete(m_strTest)    
    Timer1.Enabled = False

End Sub

答案 2 :(得分:0)

您有正确的想法使用Timer等待删除,但您尝试使用它的方式不正确。一个(System.Windows.Forms)计时器在它滴答时引发一个事件,你需要将该事件与你想要运行的代码联系起来。

另外,要组合路径的某些部分,最好使用IO.Path.Combine,并且在进行文件操作时,最好是" wrap" Try..Catch构造中的相关代码会在发生异常时处理该异常。

所以:

Option Infer On
Option Strict On

Imports System.IO

Public Class Form1
    Dim tim1 As New System.Windows.Forms.Timer
    Dim tte4 As String = "afile.txt"

    Private Sub Timer1_Tick(sender As Object, e As EventArgs)
        tim1.Enabled = False
        Dim target = Path.Combine("C:\temp", tte4)

        If File.Exists(target) Then
            Try
                File.Delete(target)
            Catch ex As Exception
                MessageBox.Show(String.Format("The file ""{0}"" could not be deleted because {1}", target, ex.Message))
            End Try
        Else
            ' the file does not exist - do something if required
        End If

    End Sub

    Private Sub SetUpTimer()
        tim1.Enabled = False
        tim1.Interval = 1000 * 15
        ' here we tie the event to the code
        AddHandler tim1.Tick, AddressOf Timer1_Tick

    End Sub

    Private Sub Form1_Click(sender As Object, e As EventArgs) Handles MyBase.Click
        ' this has the side-effect of trying to delete a file after an interval
        tim1.Enabled = True

    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SetUpTimer()

    End Sub

End Class

您没有显示创建文件的代码 - 我假设您只是单击表单来演示该想法。