IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
C#
Search C# Group:

Advanced
-Collapse +Expand C# To/From
To/FromCODEGuides
-Collapse +Expand C# Study Test
PRESTWOODCERTIFIED
-Collapse +Expand C# Store
PRESTWOODSTORE

Prestwood eMagazine

December Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingC#   Print This     
 
C#:
How I Created a C Sharp Module for Automatic Data Back Up
 
Posted 13 months ago on 11/13/2020
Take Away:

This article describes the C# coding I used to create a module for automatic data back up.

KB104921

INTRODUCTION

One of the great things about programming code is the fact that it can be harnessed to automate small, but important chores that are often overlooked. This is especially critical in a business where one typically has too much to do with insufficient time to do it. Here, I will discuss C# code I used for an actual customer of mine. It selectively performs an automated data back up operation upon entering the inventory application I designed for them.

HAS A BACK UP OPERATION OCCURRED IN THE LAST 7 DAYS?

This automated data back up code is fired upon entering the Winform’s Load Event. After the initial chores are taken care of, the back up log data file, bulog.txt, will be accessed. The last appended record to the back up log will be read since it represents the last time an automated back up was performed. Next, the month/day/year components from the last back up date will be retrieved to construct a DateTime object I call last_auto_backup_date. The last back up date will be compared to the date 7 days prior to the current date. If the date from 7 days ago is greater than or equal to the last automated back up date, then a new automated back up operation will commence. The programming below illustrates how the date manipulation is done.

  1. // this is a "load" module of a C# application for inventory
  2. // management. upon firing the "load" event from the 
  3. // main menu screen, this module will test to see if an automatic
  4. // back up of the master inventory data file, "inventory.txt" and the
  5. // detail transaction file associated with each master inventory item
  6. // have been performed in the last 7 days. if not, then it will
  7. // automatically be performed before the main menu is accessible
  8. // to the user.
  9. private void FormGeneric_Load(object sender, System.EventArgs e)  
  10. {  
  11. // declare variables used in the "load" event of the main menu.
  12. intscreen_width, screen_height, yearvar, monthvar, dayvar, a;  
  13. string stringVal, currdir, item_file_name, bkupDir, strmonthvar, strdayvar, string_file_name_backedup;  
  14. string string_file_name;  
  15. string string_auto_backup_date, string_auto_backup_description, stringError;  
  16. longsizeofdatafile, sizeofdatafile2, datafileoffset, datafileoffset2, filelengthvar;  
  17. // size of each record in the back up log data file.
  18. constlong BULEN = 32;  
  19. // size of each record in the master inventory data file.
  20. constlong MASTERLEN = 60;  
  21. // size of each record in the transaction detail data file.
  22. constlong DETAILEN = 75;  
  23. // character arrays used to manipulate the data.
  24. char[] recordatavar = newchar[MASTERLEN - 2];  
  25. char[] recordatavar_transactions = newchar[DETAILEN - 2];  
  26. char[] backup_log_data = newchar[BULEN - 2];  
  27. char[] char_auto_backup_date, char_auto_backup_description;  
  28. // set the boundaries of the menu screen.
  29. screen_width = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width;  
  30. screen_height = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height;  
  31. this.SetBounds(1, 1, screen_width, screen_height);  
  32. // set the start position of the menu screen to the center of the viewport.
  33. this.StartPosition = FormStartPosition.CenterScreen;  
  34. // get the current working directory of the installation.    
  35. currdir = Directory.GetCurrentDirectory();  
  36. currdir = currdir.Substring(0, currdir.Length - 9);  
  37. // get the date of the last auto back up event by opening
  38. // the back up log file, "bulog.txt".
  39. FileInfo datafileinfo = newFileInfo(currdir + "bulog.txt");  
  40. sizeofdatafile = datafileinfo.Length;  
  41. StreamReader streamobj = newStreamReader(currdir + "bulog.txt");  
  42. BaseStream.Seek(sizeofdatafile - BULEN, SeekOrigin.Begin);  
  43. stringVal = streamobj.ReadLine();  
  44. Close();  
  45. // use the TryParse method to split the date portion of the
  46. // string "stringVal" into month, day and year components.
  47. // adjust the parsed year component for the 21st century.
  48. int.TryParse(stringVal.Substring(0, 2), out monthvar);  
  49. int.TryParse(stringVal.Substring(3, 2), out dayvar);  
  50. int.TryParse(stringVal.Substring(6, 2), out yearvar);  
  51. yearvar = yearvar + 2000;  
  52. // put the month, day and year components into a DateTime
  53. // object called "last_auto_backup_date".
  54. DateTime last_auto_backup_date = newDateTime(yearvar, monthvar, dayvar, 0, 0, 0);  
  55. // subtract 7 days from today‘s date then see if it is greater than
  56. // or equal to the date of the last auto back up; if true then do another
  57. // one for today.
  58. DateTime today = System.DateTime.Now;  
  59. TimeSpan duration = newSystem.TimeSpan(7, 0, 0, 0);  
  60. DateTime seven_days_prior_date = today.Subtract(duration);  
  61. if(seven_days_prior_date >= last_auto_backup_date)  
  62. {  
  63. .  
  64. .  
  65. .  

PREP WORK FOR FILE NAMING CONVENTION IS NEEDED TO PREVENT OVERWRITES ON EXISTING BACKED UP DATA FILES

Before the actual data back up operation can begin, a little preparation work is needed. The backed up file name for the master inventory data file and each of its detail inventory data files must be constructed. This must be done in a way that makes them unique so they won’t overwrite any other master inventory data file or detail inventory data files in the target back up directory. For the master inventory data file, I concatenate the string “inventory_” to a 2 character current month string followed by a 2 character current day string and then a 4 character current year string and then the string .txt. For example, if today’s date is 02/06/2013 and an automatic back up operation is performed, then the master inventory file name in the target back up directory would be inventory_02062013.txt. A similar methodology is used to name the detail inventory data files that are being copied. Let’s say the detail inventory data file name as found in a given master inventory record is item0012. Using our example, its counterpart back up data file in the target back up directory would be item0012_02062013.txt. Incorporating the current date into the back up data file’s naming convention ensures that it will always be unique and no previously backed up data files will ever be overwritten.

LOOP AROUND THE MASTER INVENTORY DATA FILE TO PROCESS EACH DETAIL INVENTORY DATA FILE

The first phase of the back up operation will be to back up the master inventory data file itself to the target directory. The second phase will sequentially loop around the source master inventory data file to retrieve each of the file names of the detail inventory data files. Each detail inventory data file contains the inventory transactions that occurred for a particular part number. Before this data file will be copied to the target destination, the program will verify that it does exist on the source drive. If this is not true, the program will advance to the next sequential detail inventory data file that is contained in the master inventory data file.

After verification, the source detail inventory data file will be written to its counterpart back up detail inventory data file in the target back up directory. After the source detail inventory data file has been written, its file stream will be closed. Then the program will repeat this for each successive inventory detail transaction file. Lastly, the source master inventory data file’s stream will then be closed after the last detail transaction data file has been backed up.

HANDLING CORRUPTED DETAIL INVENTORY DATA FILES

Each detail inventory data file size will be measured to ensure that each of its records are of DETAILEN size. This is a methodology I use to make sure no extra/errant characters have been inserted into a given data file. This would throw a wrench into the back up process and make the back up operation abort. If the code does detect an erroneous record, the file name is then concatenated to a variable I call stringError. If there is a problem with one or more detail transaction data files, then this variable will be displayed as a message at the end of processing. Also, the back up log data file will not be updated with a successful back up operation record. Please click here to see the entire code readout.

UPDATE THE BACK UP LOG DATA FILE

After the automated back up has been completed, the program will note this in the back up log data file on the assumption no corrupted detail inventory data files were detected during the back up operation. If even one is detected, the back up log will not be updated and instead a message will appear on screen noting the detail inventory data file(s) that were not processed due to the presence of non-homogenous or errant data records that were encountered during processing.

The back up log is updated so future date comparisons between the date of the last back up operation and 7 days prior to the current date will be accurate. This is actually a pretty simple task. The string AUTO-BACKUP__PERFORMED will be concatenated to the end of the string equivalent of the current date. This information will then be appended to the end of the back up log data file, bulog.txt.

CONCLUSION

Software development is a vehicle to increased efficiency in the workplace. Given today’s demands on staff and their limited time, manual methods of getting tasks done even on a computer may not be the best choice. My automated back up code for the inventory application I created is just one example of how programming can be used to liberate employees from mundane tasks so they can focus on more important issues at hand.


Comments

1 Comments.
Share a thought or comment...
Comment 1 of 2

I am interested to get some new information about this Module to recover my deleted data which can not take backup. Thanks for sharing this useful news with us.  

 dissertation writing services
Posted 13 months ago

Comment 2 of 2

This is not used to recover deleted/lost files. It only does back up.

Posted 12 months ago
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P184A1
Enter key:
Article Contributed By NE Ohio Computer Guy:

Please visit my software developer website for more information about my services. I offer application development as well as Android app coding services. My developer skills are best suited to dealing with custom software projects. I can perform programming for Corel Paradox as well as C# and PHP.

In my local area of northeast Ohio, I can cater to computer repair and "fix my computer" issues. And don‘t forget to check out my YouTube Channel. It is full of cool videos and has something for everyone!

Use my contact web page today to reach me about any software design ideas you have.

Visit Profile

 KB Article #104921 Counter
367
Since 11/13/2020
Go ahead!   Use Us! Call: 916-726-5675  Or visit our new sales site: 
www.prestwood.com


©1995-2021 Prestwood IT Solutions.   [Security & Privacy]