Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. backup

backup

Scheduled Pinned Locked Moved C#
question
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BLaZiNiX
    wrote on last edited by
    #1

    I want to create a Backup program for my PC and I want to know how can I write into a file all the filepath I backuped and all the data of each of these files. I want to write all that thing in one file and after I want to reopen the file I created and restore all the the path with the associated data to his correct place (the same path as in the file I created) Thanks a lot Jonathan

    J 1 Reply Last reply
    0
    • B BLaZiNiX

      I want to create a Backup program for my PC and I want to know how can I write into a file all the filepath I backuped and all the data of each of these files. I want to write all that thing in one file and after I want to reopen the file I created and restore all the the path with the associated data to his correct place (the same path as in the file I created) Thanks a lot Jonathan

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      That all depends on how your program is structured :) If you are just starting what I would do is create a single class or struct that represents one file in the back up. Give it the Serializable attribute and implement ISerializable. When you implement ISerializable there are two things you must do: 1) provide a GetObjectData method, which puts the data into the serialization stream (ie a file) 2) provide a constructor that takes the same parameters as GetObjectData, except this takes the data out of the file and loads your class. When you serialize the class (GetObjectData is called) store the original path/filename and then load the contents of the file in there. When you deserialize the class (the constructor is called) you pull out the original path/filename and then take the data out of the stream and place it in the file. Then all your program has to do is manage these objects :) James Sonork ID: 100.11138 - Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

      B 1 Reply Last reply
      0
      • J James T Johnson

        That all depends on how your program is structured :) If you are just starting what I would do is create a single class or struct that represents one file in the back up. Give it the Serializable attribute and implement ISerializable. When you implement ISerializable there are two things you must do: 1) provide a GetObjectData method, which puts the data into the serialization stream (ie a file) 2) provide a constructor that takes the same parameters as GetObjectData, except this takes the data out of the file and loads your class. When you serialize the class (GetObjectData is called) store the original path/filename and then load the contents of the file in there. When you deserialize the class (the constructor is called) you pull out the original path/filename and then take the data out of the stream and place it in the file. Then all your program has to do is manage these objects :) James Sonork ID: 100.11138 - Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971

        B Offline
        B Offline
        BLaZiNiX
        wrote on last edited by
        #3

        do you have a sample code, i'm not familiar with this knid of stuff ? Thanks

        J 1 Reply Last reply
        0
        • B BLaZiNiX

          do you have a sample code, i'm not familiar with this knid of stuff ? Thanks

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          I don't have anything premade but I can come up with a general solution that will need a little tweaking :)

          [Serializable()]
          public class BackupFile : ISerializable
          {
          private string _filename = "";
          private byte[] _filedata = null;

          public string Filename
          {
          	get { return \_filename; }
          	set { \_filename = value; }
          }
          
          public byte\[\] FileData 
          {
          	get { return \_filedata; }
          	set { \_filedata = value; }
          }
          
          // Don't allow default constructor use
          private BackupFile() { }
          
          public BackupFile(string filename)
          {
          	Filename = filename;
          	FileData = null;
          }
          
          public BackupFile(SerializationInfo info, StreamingContext context)
          {
          	byte \[\] tempB = new byte\[1\];
          
          	Filename = (string) info.GetValue("filename", typeof(string));
          	FileData = (byte\[\]) info.GetValue("filedata", temp.GetType());
          
          	WriteFileToDisk();
          
          	FileData = null; // "free" the memory used by the file
          }
          
          public void GetObjectData(SerializationInfo info, StreamingContext context)
          {
          	FileData = LoadFileFromDisk();
          
          	info.AddValue("filename", Filename, Filename.GetType());
          	info.AddValue("filedata", FileData, FileData.GetType());
          
          	FileData = null;  // "free" the memory used by the file
          }
          
          private void WriteFileToDisk()
          {
          	// Open the file specified by Filename and write the bytes stored in 
          	// FileData to it
          }
          
          private void LoadFileFromDisk()
          {
          	// Open the file specified by Filename and copy the data from the stream
          	// to FileData
          }
          

          }

          Now you may wish to also offer compression as well, there is a .NET implementation of gzip/zip available at ICSharpCode.NET. The compression library is GPL, but there is an exception that reads "As a special exception, if you link this library with other files to produce an executable, this library does not by itself cause the resulting executable to be covered by the GNU General Public License. This exception does not however invalidate any other reasons why the executable file might be covered by the GNU General Public License." James [Edit: AHA! I finally figured out how to get the "extra" linebreaks to appear... They can't be blank lines :) Each "blank" line needs a space on it. Pardon me while I celebrate :jig:] Sonork ID: 100.11138 - Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups