【问题标题】:Automate a task with VB.net使用 VB.net 自动执行任务
【发布时间】:2014-02-21 10:12:59
【问题描述】:

我有这段代码可以自动备份文件夹,它做得很好。但是,我想通过让它每天下午 12 点自动备份来提高它的档次。 Ant 提示如何解决此问题将不胜感激。

Imports System.IO
Imports System.IO.Compression
Public Class Form1

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

    Dim dstr As String
    Dim mstr As String
    Dim ystr As String
    Dim folstr As String
    Dim dsumstr As String

    dstr = DateTime.Today.ToString("dd")
    mstr = DateTime.Today.ToString("MM")
    ystr = DateTime.Today.ToString("yyyy")
    dsumstr = ystr & "-" & mstr & "-" & dstr
    folstr = "Y:\server1\Fileserver-" & dsumstr
    Try
        My.Computer.FileSystem.CreateDirectory(folstr)

        My.Computer.FileSystem.CreateDirectory(folstr & "\SHARE-AC")
        My.Computer.FileSystem.CopyDirectory("D:\SHARE-AC", folstr & "\SHARE-AC")



        Label1.Text = "Back up DATE   " & dsumstr & "  Complete"
    Catch ex As Exception
        Label1.Text = (ex.Message)
    End Try

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Me.Close()
End Sub
End Class

【问题讨论】:

    标签: vb.net windows backup scheduled-tasks


    【解决方案1】:

    对于我们的 Windows 服务器,我们使用任务计划程序。你可以阅读更多here

    【讨论】:

      【解决方案2】:

      添加一个计时器 - 如下:

      Dim WithEvents Timer1 As Timer
      

      设置你的变量(或从任何地方读取它们)。

      Dim runtimestring As String = "12:00 PM"
      Dim nextruntime As Date
      

      在启动时将间隔设置为下一个运行时并启动计时器:

      Sub New()
          'set up time interval
          If Now.TimeOfDay > Date.Parse(runtimestring).TimeOfDay Then
              'set nextruntime to tomorrow
              nextruntime = Date.Parse(runtimestring).AddDays(1)
          Else
              'set nextruntime to today
              nextruntime = Date.Parse(runtimestring)
          End If
          Timer1.Interval = nextruntime.Subtract(Now).TotalMilliseconds
          Timer1.Start()
      End Sub
      

      在 Tick 事件中,停止计时器根据需要执行您的功能/子,然后重新设置间隔并再次启动计时器。

      Sub Timer1_Tick() Handles Timer1.Tick
          Timer1.Stop()
          'Add your functions and sub's here.....
      
      
          'set nextruntime to tomorrow
          nextruntime = Date.Parse(runtimestring).AddDays(1)
          Timer1.Interval = nextruntime.Subtract(Now).TotalMilliseconds
          Timer1.Start()
      End Sub
      

      【讨论】:

      • 为什么不加评论就投反对票? - 如果您发表评论,我希望可以改进答案!
      猜你喜欢
      • 2020-12-09
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多