0

I'm trying to write a little backup-tool in C#. The files I want to backup (and zip) are stored like this:

c:\1\2\3\files\backupfile1.txt c:\1\2\3\files\backupfile2.txt

but also like this:

c:\1\2\3\files\1\backupfile3.txt

The user has to specify the path "C:\1\2\3". My program now searches through all subdirectories and puts it in an array. After that, it displays a list of all entries in a list with checkboxes. The user then has to check which files he wants to save. When the "Save" button is pressed, all checked files are stored in a zip-file but with full pathnames. This is a problem when I want to restore them on another system, because the folders can be different. The destination could look like this:

C:\1\files\backupfile1.txt C:\1\files\1\backupfile3.txt

So my problem is, that I have to store only part of the folders in the zip, relative to the path the user has to specify. Since I have only very little experience with complex programs like this, I'm lost. I have seen that it is possible to split a string. I thought that it might be possible to read all files and before zipping them, split the path at every \ and delete every part before the "files" directory and then zip everything with this split directory, but I can't figure out how. Is this possible or is there an easier way to do this? Important is that the folder structure after the "files"-folder has to stay intact.

EDIT: The function to display the checkbox list looks like this:

private void bt_aktualisieren_Click(object sender, EventArgs e)
        {
            string folder = Properties.Settings.Default.folder;
            string[] directories = folder.Split(Path.DirectorySeparatorChar);
            bool temp = false;

            for (int i = 0; i < directories.Count(); i++)
            {
                if (directories[i] == "files")//files is your Directory with files and subdirs which you want to backup
                    temp = true;
            }
            if (temp == true)
            {
                string[] files = (from f in Directory.GetFiles(@folder += "\\backup", "*", SearchOption.AllDirectories)
                                    where f.EndsWith(".txt")
                                    select f).ToArray();
                                    checkedListBox1.Items.AddRange(files);
            }
        }

1 Answers1

0

You can split your path with:

string[] directories = path.Split(Path.DirectorySeparatorChar);

Then you can check the array for the 'File-Folder' and all directory names after your 'File-Folder' can be in your file name.

EDIT:

SAMPLE - NOT TESTED

string mypath = c:\1\2\3\files\backupfile1.txt
string[] directories = mypath.Split(Path.DirectorySeparatorChar);

directories[0] = C:
directories[1] = 1
direcotries[2] = 2

and so on...

bool temp = false;

for(int i = 0; i<directories.Count(); i++)
{
   if(directories[i] == "files")//files is your Directory with files and subdirs which you want to backup
      temp = true;
   }
   if(temp == true)
   {
      // ADD directories[i] to checkbox list
   }
}
Niklas
  • 27
  • 7