Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6778767
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:18:18+00:00 2026-05-26T16:18:18+00:00

I am doing backup schedule every day. If any backup file is not created

  • 0

I am doing backup schedule every day. If any backup file is not created within 24 hours, I want to create new backup file (executing backupdatabase() function).

In addition, I have used a timer, so that every hour it will be checked whether the file is created or not. And after 24 hours it will create a new file (executing backupdatabase() function).

File checking is done by using File.GetcreationTime. Whether the file is created or not.

For that I have done like this…

public partial class BackupForm : Form
{
    private static System.Timers.Timer _timer;
    private Int32 _hours = 0;
    private const Int32 RUN_AT = 10;

    private void BackupForm_Load(object sender, EventArgs e)
    {
        var today = DateTime.Now;
        _hours = (24 - (DateTime.Now.Hour + 1)) + RUN_AT;
        _timer = new Timer { Interval = _hours * 60 * 60 * 1000 };
        _timer.Elapsed += new ElapsedEventHandler(Tick);
        _timer.Start();
    }

    void Tick(object sender, ElapsedEventArgs e)
    {
        _timer.Stop();
        var name = "localhost";
        const string path = @"C:\Creation\Name\backupdb\";
        var listfiles = Directory.GetFiles(@"C:\Creation\Name\backupdb\", "backup-*.zip").ToList();
        var files = listfiles.Select(Path.GetFileName).ToList();
        var dt = DateTime.Now;
        foreach (var file in files)
        {
            var creationtime = File.GetCreationTime(file);
            var diff = DateTime.Now.Subtract(creationtime);
            if (diff.Hours > 24 && diff.Days < 2 && creationtime.Month == dt.Month && creationtime.Year == dt.Year && name == "localhost" && _hours == 24)
            {
                backupDatabase();
            }
            else if (_hours != 24)
            {
                _hours = 24;
                _timer.Interval = _hours * 60 * 60 * 1000;
            }
        }
        _timer.Start();
    }
}

But it doesn’t work, it does not check the file creation time and even it does not create the file after 24 hours.

How to check the file with creation time along with timer for every one hour and after 24 hours how to create the file?

Any help would be very helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T16:18:19+00:00Added an answer on May 26, 2026 at 4:18 pm

    In order to have a method that runs every hour you need to assign a one hour interval to the timer. Timer.Interval is expecting a value in milliseconds so:

    // 1000 ms per second, 60 seconds per minute, and 60 minutes per hour
    _timer.Interval = 1000 * 60 * 60;
    

    This will make the timer “tick” every hour. Inside the Timer_Tick function you should simply check for file dates that are within the last hour:

    var creationtime = File.GetCreationTime(file);
    var diff = DateTime.Now.Subtract(creationtime);
    
    if (diff.TotalHours <= 1)
    {
        // File was created within the last hour
    }
    

    However, I believe what you are really trying to achieve a solution where you simply create a backup if one has not been created within 24 hours. To achieve this you would use the following:

    1) Create a new method called BackupIfNecessary()

    private void BackupIfNecessary()
    {
        var name = "localhost";
        const string path = @"C:\Creation\Name\backupdb\";
        var listfiles = Directory.GetFiles(@"C:\Creation\Name\backupdb\", "backup-*.zip").ToList();
        var files = listfiles.Select(Path.GetFileName).ToList();
    
        DateTime lastBackup = DateTime.MinValue;
    
        foreach (var file in files)
        {
            var creationtime = File.GetCreationTime(file);
    
            // Check if the creation date for this file is the "latest"
            if (creationtime > lastBackup)
            {
                // Store as the "latest" time
                lastBackup = creationtime;
            }
        }
    
        var diff = DateTime.Now.Subtract(lastBackup);
        if (diff.TotalHours >= 24)
        {
            // The last backup file was created over 24 hours ago; You should create the file
        }
    }
    

    2) When your form loads, execute the backup function for the first time by calling BackupIfNecessary in Form_Load (so you don’t wait full 24 hours for the first run)

    3) Set a timer interval for 24 hours in Form_Load:

    // 1000 ms per second, 60 seconds per minute, and 60 minutes per hour, and 24 hours per day
    _timer.Interval = 1000 * 60 * 60 * 24;
    

    4) Inside of Timer_Tick simply call BackupIfNecessary() which will check if a backup has been created within the last 24 hours, if not it will create one

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're doing a full backup of each of our production databases every night. We
I'm doing a webapp and need a backup plan. Here's what I've got so
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
I'm attempting to create a new task in the Windows Task Scheduler in C#.
i am doing sql server backups like this in script: BACKUP DATABASE databasename TO
I want to backup the database as gzip, but it doesn't work. This is
Exchange 2010 does not support the ESE API for doing backups like it did
I am using this code for backup database from .mdf file. Backup databaseBackup =
We're not doing real web development. We get our HTMLs from our designers, and
I haven't found a backup (synchronization) program which does what I want so I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.