0

Sounds like read-only files, right? well not really.

So my goal is to make a backup of all the files from an application I have build by simply compressing them to a .zip file. First I collect all files and put them in another folder (I don't need to backup the .exe file for example). Then I compress them using IO.compression.ZipFile

ZipFile.CreateFromDirectory(backupDirectory, ZipDirectory);

Later, when user would like to restore a backup, I first delete the existing files and directories, then extract it:

ZipFile.ExtractToDirectory(openFileDialog1.FileName, installDirectory);

Problem: Files are now read-only, One file is a .mdf file, I cannot make changes to the tables anymore, which makes the app useless. I've tried doing this:

public void Recurse(DirectoryInfo directory)
    {
        foreach (FileInfo fi in directory.GetFiles())
        {
            //fi.Attributes |= FileAttributes.ReadOnly; //this didn't work eather
            fi.IsReadOnly = false;
        }

        foreach (DirectoryInfo subdir in directory.GetDirectories())
        {
            Recurse(subdir);
        }
    }

from this question. Didn't work. And I don't expect it to work since the 'Read-only' flag is not checked on the .mdf properties. Also when looking into the Security tab:

SYSTEM, Admin and User are the same, what I am missing is the groupname 'Everyone' with all checks set to Allowed. After manually setting this group again, I see some improvements, however still getting other errors like unable to login, login failed for the user [current user]

Edit: issue with the above security tab was because I used that Recurse method. Made things worse. So using the file.IsReadOnly = false for all files is not a good solution.

Question: How do I correctly Compress and decompress files without changing it's attributes / settings / permissions. **Or: ** How do I set the correct attributes for the files again, after decompressing.

Community
  • 1
  • 1
CularBytes
  • 9,924
  • 8
  • 76
  • 101
  • possible duplicate of [Extracted Files are always read-only](http://stackoverflow.com/questions/10715768/extracted-files-are-always-read-only) – MethodMan Jan 23 '15 at 16:49
  • Read what I have tried, it matches the answers in that question. That is where I got the first solution from, which didn't work. – CularBytes Jan 23 '15 at 17:05
  • Since the files are not being set to read-only (which is really the only thing that `ZipFile` might set, and then only if that attribute were copied into the archive when it was created), you are almost certainly dealing with a security issue and not a programming issue. You just need to make sure that the process unpacking the archive is run by the account that you want to be the owner of the unpacked files, and/or that you unpack the files into a directory where the access rights match what you want (i.e. files inherit from the directory, and users you want have access). – Peter Duniho Jan 23 '15 at 19:31

1 Answers1

0

I found the solution.

In an other question/solution I mentioned, he said to change to fileinfo.IsReadOnly = false; instead of:

fileInfo.Attributes = FileAttributes.Normal

For me it was the other way around.

So with this parameter:

DirectoryInfo di = new DirectoryInfo(installDirectory);
Recurse(di);

I used this:

public void Recurse(DirectoryInfo directory)
{
    foreach (FileInfo fi in directory.GetFiles())
    {
        fi.Attributes = FileAttributes.Normal;
    }

    foreach (DirectoryInfo subdir in directory.GetDirectories())
    {
        Recurse(subdir);
    }
}

Edit:

After some more time trying to find a better solution, I came across this topic. which solved it completely.

Community
  • 1
  • 1
CularBytes
  • 9,924
  • 8
  • 76
  • 101