backup
-
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
-
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
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
-
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
-
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