Elixir Elixir - 3 months ago 25
C# Question

How to create backup of ms access in folder?

I want to create backup of ms access database with

.bk
extension with c# code And the backup should automatically save in Day Wise folder like If I click on
backup
today, then it should automatically create back in
Wednesday
Folder. How to do this?

Answer

Backup of MS Access database is a file copy.

Starting from a baseDirectory, with Path.Combine you can build a fullDirectory name containing the baseDirectory and the day. After that you can copy the file (File.Copy) to that directory (eventually using Path.Combine to build a fullPath of the file). Don't forget to Dispose the connection before copying the file and be sure the no one is using the file when you make the copy.

EDIT

using System;
using System.IO;

public class Program
{
    public static void Main()
    {

        string baseDirectory = "C:\\Insert\\Your\\Base\\Directory";
        string mdbSourceFilePath = "C:\\Directory\\Of\\mdb\\MyAccessDb.accdb";
        string mdbTargetFileName = "MyAccessDb.bk";

        string fullDirectory = Path.Combine(baseDirectory, DateTime.Today.ToString("dddd"));
        if (!Directory.Exists(baseDirectory))
            Directory.CreateDirectory(baseDirectory);
        if (!Directory.Exists(fullDirectory))
            Directory.CreateDirectory(fullDirectory);

        string mdbTargetFilePath = Path.Combine(fullDirectory, mdbTargetFileName);

        File.Copy(mdbSourceFilePath, mdbTargetFilePath);


    }


}
Comments