C# ile daha önce geliştirdiğim ve internette çeşitli yerlerde yayınlanan Backup Console uygulamasının  solution halini buradan indirebilirsiniz.

Programda yedeği alınacak klasör içerisindeki dizin ve alt klasörler, hedef olarak belirtilen yedek dosyasının içerisine binary olarak ve başlık bilgileri düzenlenerek eklenirler.

Yedekden geri alma işleminde ise bu başlık bilgilerine göre dosyalar ve dizinler yeni hedef klasöre oluşturulur.

Ayrıca programda Xor ile şifreleme de yapmaktayım. Xor’u birden fazla kullanabilirsiniz. Birden fazla kullanmanız kırılmasını imkansız hale getirecektir.

Yedekleme ve geri alma işlemini yapan Yedek.cs :

/*
 * Dosyalarınızda meydana gelecek olası hasarlardan programı yazan
*kişi ve programı yayınlayanlar sorumlu tutulamaz.
 * */
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace denemeler
{
    enum YedekParam
    {
        YEDEKLE,
        GERIAL
    }
    enum IcerikParam
    {
        DOSYA,
        DIZIN
    }
    class Yedek
    {
        private string yedekdosya;
        private string icerik;
        private static Yedek _yedek;
        private FileStream fsBck;
        private FileStream fsCon;
        private string yerelPath;
        private char[] splitter1 = new char[] { '\\'};
        private Yedek(string bck,string icrk,YedekParam param)
        {
            switch(param)
            {
                case YedekParam.YEDEKLE:
                    fsBck = new FileStream(bck,FileMode.Create,FileAccess.Write);
                    break;
                case YedekParam.GERIAL:
                    fsBck = new FileStream(bck,FileMode.Open,FileAccess.Read);
                    break;
            }

            this.yedekdosya = bck;
            this.icerik = icrk;
        }

        static public Yedek GetYedek(string bck, string icrk,YedekParam param)
        {
            if (_yedek == null)
                _yedek = new Yedek(bck, icrk,param);

            return _yedek;
        }

        public void Yedekle()
        {
            this.gozat(this.icerik);
            this.fsBck.Close();
        }
        public void GeriAl()
        {
            byte[] reserv;
            byte[] size;
            byte[] neyi;
            byte[] Path;
            byte[] okunan;
            Encoding enc = Encoding.GetEncoding(1254);
            DirectoryInfo drinfo = new DirectoryInfo(this.icerik);

            if (!drinfo.Exists)
                Directory.CreateDirectory(this.icerik);

            while (this.fsBck.ReadByte() >= 0)
            {
                this.fsBck.Seek(-1, SeekOrigin.Current);

                reserv = new byte[300];
                size = new byte[30];
                Path = new byte[300];
                neyi = new byte[1];
                this.fsBck.Read(reserv, 0, 300);
                this.fsBck.Read(size, 0, 30);
                this.fsBck.Read(neyi, 0, 1);
                this.fsBck.Read(Path, 0, 300);

                string file = enc.GetString(reserv).Trim();
                file = file.Replace("\0", "");

                Console.WriteLine(file + " geri aliniyor...");
                string _neyi = enc.GetString(neyi).Trim();

                string _path = enc.GetString(Path).Trim();
                _path = _path.Replace("\0","");

                switch (_neyi)
                {
                    case "F":
                        break;
                    case "D":

                        Directory.CreateDirectory(drinfo.FullName +"\\"+_path+"\\"+ file);
                        Console.WriteLine(drinfo.FullName + "\\" + _path + "\\" + file + " dizini AÇILDI");
                        continue;
                        //break;
                }
                string _size = enc.GetString(size);
                int __size = Convert.ToInt32(_size);
                okunan = new byte[__size];
                this.fsBck.Read(okunan, 0, okunan.Length);

                FileStream fn;
                try
                {
                    fn = new FileStream(drinfo.FullName+ _path + @"\" + file, FileMode.Create, FileAccess.Write);
                    fn.Write(okunan, 0, okunan.Length);
                    fn.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    return;
                }
            }
            this.fsBck.Close();
            Console.WriteLine("GeriAl tamamlandi!");
        }
        private void yedekleBunu(string path, string name, string sized, IcerikParam _ic)
        {
            byte[] reserv;
            byte[] size;
            byte[] neyi;
            byte[] Path;
            Encoding enc = Encoding.GetEncoding(1254);

            switch (_ic)
            {
                case IcerikParam.DIZIN:
                    neyi = enc.GetBytes("D");

                    reserv = new byte[300];
                    Array.Copy(enc.GetBytes(name), reserv, enc.GetBytes(name).Length);

                    size = new byte[30];
                    Array.Copy(enc.GetBytes(sized), size, enc.GetBytes(sized).Length);

                    Path = new byte[300];
                    Array.Copy(enc.GetBytes(path.Replace(this.yerelPath, @"\")), Path, enc.GetBytes(path.Replace(this.yerelPath, @"\")).Length);
                    //Console.WriteLine(path.Replace(this.yerelPath, @"\"));
                    fsBck.Write(reserv, 0, 300);
                    fsBck.Write(size, 0, 30);
                    fsBck.Write(neyi, 0, 1);
                    fsBck.Write(Path, 0, 300);
                    fsBck.Flush();
                    break;
                case IcerikParam.DOSYA:
                    neyi = enc.GetBytes("F");

                    reserv = new byte[300];
                    Array.Copy(enc.GetBytes(name), reserv, enc.GetBytes(name).Length);

                    size = new byte[30];
                    Array.Copy(enc.GetBytes(sized), size, enc.GetBytes(sized).Length);

                    Path = new byte[300];
                    Array.Copy(enc.GetBytes(path.Replace(this.yerelPath, @"\")), Path, enc.GetBytes(path.Replace(this.yerelPath, @"\")).Length);

                    fsCon = new FileStream(path + @"\" + name, FileMode.Open, FileAccess.Read);

                    fsBck.Write(reserv, 0, 300);
                    fsBck.Write(size, 0, 30);
                    fsBck.Write(neyi, 0, 1);
                    fsBck.Write(Path, 0, 300);
                    int okunanbyte;
                    while ((okunanbyte = fsCon.ReadByte()) >= 0)
                        fsBck.WriteByte((byte)okunanbyte);
                    Console.WriteLine(name+" yedeklendi...");
                    fsCon.Close();
                    fsBck.Flush();

                    break;
            }
        }
        public static void sifrele(string kaynak, string hedef, string sifre)
        {
            int XOR = 0;
            for (int i = 0; i < sifre.Length; i++)
            {
                //XOR = XOR + (int)(sifre[i] * 10);
                XOR = XOR + (int)(sifre[i] * (i + 1));
            }
            FileStream fsKaynak = new FileStream(kaynak, FileMode.Open);
            FileStream fsHedef = new FileStream(hedef, FileMode.Create, FileAccess.Write);
            int okunanByte;
            byte sifreliByte;

            while ((okunanByte = fsKaynak.ReadByte()) != -1)
            {
                sifreliByte = (byte)((int)okunanByte ^ XOR);
                fsHedef.WriteByte(sifreliByte);
            }
            fsKaynak.Close();
            fsHedef.Close();
        }

        private void gozat(string path)
        {

            DirectoryInfo drinfo = new DirectoryInfo(path);
            if (!drinfo.Exists)
            {
                Console.WriteLine(path+" dizin bulunamadi");
                return;
            }
            if (this.yerelPath == null)
                this.yerelPath = drinfo.FullName.ToString();

            //Console.WriteLine(this.yerelPath);
            FileInfo[] fs = drinfo.GetFiles();
            foreach (FileInfo _fs in fs)
            {
                yedekleBunu(drinfo.FullName, _fs.Name, ((FileInfo)_fs).Length.ToString(), IcerikParam.DOSYA);
                //Console.WriteLine("Dizin Path: " + drinfo.FullName + " Dosya Name: " + _fs.Name);
            }

            DirectoryInfo[] dr = drinfo.GetDirectories();
            foreach (DirectoryInfo _dr in dr)
            {
                yedekleBunu(drinfo.FullName, _dr.Name, "0", IcerikParam.DIZIN);
                //Console.WriteLine("Dizin Path: "+drinfo.FullName+" Dizin Name: "+_dr.Name);
                if (_dr != null)
                    gozat(_dr.FullName);
            }
        }

    }
}
Kategoriler: CSharp