Small project for full disk backup (C #)

Source: Internet
Author: User

App. CS

Using system;
Using system. IO;

Namespace com. todayisp. bakup
{
/// <Summary>
/// Summary of the app.
/// </Summary>
Public class app
{
// List all directories under a disk, excluding files.
// Directory path pathstr, such as E :/
// Returns an array composed of directories.
Public String [] directorys (string pathstr)
{
If (pathstr = NULL) return NULL;
If (! Directory. exists (pathstr ))
{
Console. writeline ("error: filepath not exist --" + pathstr );
Return NULL;
}
Console. writeline ("Start Check path ");
String [] filestrs = directory. getdirectories (pathstr );
For (INT I = 0; I <filestrs. length; I ++)
{
Console. writeline (filestrs [I]);
}
Return filestrs;
}

// Compress the contents, and execute rar.exe + command line.
// Rarname: the path and name of the rarfile to be compressed, for example, E:/abc.rar
// Dirname: directory to be compressed, such as E:/ABC
Public bool Bak (string rarname, string dirname)
{
Bool blnrst = false;
String strarg = "a-r {0} {1 }";
System. Diagnostics. PROCESS p = new system. Diagnostics. Process ();
P. startinfo. filename = @ "E:/rar.exe"; // EXE, bat and so on
P. startinfo. windowstyle = system. Diagnostics. processwindowstyle. normal;
P. startinfo. Arguments = string. Format (strarg, rarname, dirname );
Try
{
P. Start ();
P. waitforexit ();
P. Close ();
Blnrst = true;
}
Catch
{
}
Return blnrst;
}

// Compress logs. Each compressed folder is written into a log file.
// Filestr Log Path, content log Content
Public bool writelog (string filestr, string content)
{
Try
{
Streamwriter Sw = new streamwriter (filestr, true, system. Text. utf8encoding. utf8 );
Sw. writeline (content );
Sw. Close ();

Return true;
}
Catch
{
Return false;
}
}

// Read the log, analyze the log, and obtain the last backup time, so that you can choose whether to operate
// Logpath, which is the directory name to be backed up by dirname.
Public bool readlog (string logpath, string dirname, int intdate)
{
Try
{
String STR;
Streamreader din = file. opentext (logpath );
While (STR = din. Readline ())! = NULL)
{
String [] STRs = Str. Split ('');
If (STRs [1] = dirname. tolower ())
{
Console. writeline (STRs [0] + ":" + STRs [1]);
Console. writeline (datetime. Today. tow.datestring ());
If (convert. todatetime (datetime. Today. tow.datestring () <convert. todatetime (STRs [0]). adddays (intdate ))
{
Din. Close ();
Return false;
}
}
}
Din. Close ();
Return true;
}
Catch {
Return false;
}
}
}
}

 

// Class1.cs program entry

Using system;
Using system. IO;
Using system. configuration;

Namespace com. todayisp. bakup
{
/// <Summary>
/// Summary of class1.
/// </Summary>
Class class1
{
/// <Summary>
/// Main entry point of the application.
/// </Summary>
[Stathread]
Static void main (string [] ARGs)
{
//
// Todo: Add code here to start the application
//
App = new app ();

String pathstr = configurationsettings. deleettings ["dir"];
String maxcontent = configurationsettings. deleettings ["maxcontent"];
String logpath = configurationsettings. receivettings ["logpath"];
Int bakupday = convert. toint32 (configurationsettings. receivettings ["bakupday"]);
String [] dir = app. directorys (pathstr );
If (DIR = NULL) return;

Long Content = 0;

For (INT I = 0; I <dir. length; I ++)
{
Try
{
If (content> = convert. toint32 (maxcontent) return;
String dirname = dir [I]. Trim ();
String rarpath = dirname + ". rar ";
Int Pos = rarpath. lastindexof (@"/");
String rarname = rarpath. substring (Pos + 1, rarpath. Length-pos-1 );

If (App. readlog (logpath, dirname, bakupday ))
{
Bool bakup = app. Bak (rarpath, dirname );
Bool bakup2 = app. writelog (logpath, datetime. Today. to1_datestring () + "" + rarname + "" + dirname. tolower ());
Console. writeline ("bakup" + dirname + "to" + rarname + "" + bakup + ". Write to log" + bakup2 );
Fileinfo Fi = new fileinfo (rarpath );
Content = content + fi. length;
Console. writeline (content + "bytes ");
}
Else
{
Console. writeline ("No need to bukup" + dirname );
}
}
Catch (exception ex)
{
Console. writeline (ex. Message );
}
}

}

}
}

 

// Bakup.exe. config configuration file

<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<! -- Application specific settings -->

<System. Web>
<Customerrors mode = "off"/>
</System. Web>

<Deleetask>
<Add key = "dir" value = "E:/Rui"/>
<Add key = "maxcontent" value = "2000"/>
<Add key = "logpath" value = "E:/abc.txt"/>
<Add key = "bakupday" value = "1"/>
</Appsettings>
</Configuration>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.