0

I'm extracting an ISO and then copying a folder from the extracted ISO. The problem is that the extracted files are read-only. I've tried changing it from the properties menu, and from code in c#. Neither worked.

The code I used for extracting the ISO is in another question: extract ISO with winrar automatically with c# or batch

I'm looking for a way to either change the attributes of the extracted ISO so that I can copy from its subfolders, or to just change the sub folders permissons.

Thanks in advance

UPDATE

new code

 string[] folderToName = txtCopyFrom.Text.Split('\\');
        string isoName = folderToName[folderToName.Length - 1];
        isoName = isoName.Remove(isoName.Length - 4, 4);

        string copyTestFrom = txtCopyTo.Text + @"\"+ isoName + @"\Test\subTest";
        string[] folderName = txtCopyFrom.Text.Split('\\');
        string folderTestTo = folderName[folderName.Length - 1];
        folderTestTo = folderTestTo.Remove(folderTestTo.Length - 4, 4);
        string copyTest = txtCopyTo.Text;
        System.IO.Directory.CreateDirectory(copyTest);

        DirectoryInfo di = new DirectoryInfo(copyTestFrom);
        di.Attributes &= ~FileAttributes.ReadOnly;

        foreach (FileInfo fi in di.GetFiles())
        {
            fi.IsReadOnly = false;

            string destinationPath = Path.Combine(copyTest, Path.GetFileName(copyTestFrom));
            File.Copy(copyTestFrom, destinationPath);
        }
        MessageBox.Show("Files Copied");   

The files in subTest are not read only, only the folder itself is.

Destination path goes to C:\users\mydocs\ISODump\subTest

After access denied exception is thrown I can still copy the folder manually

UPDATE 2

workaround

Found a work around, for my purposes. directory.move achieves the purpose I wanted by moving the folder, instead of copying it.

Thanks

Community
  • 1
  • 1
ELSheepO
  • 305
  • 3
  • 9
  • 26
  • `I've tried changing it from the properties menu, and from code in c#. Neither worked.`. If you can't change it from the properties menu, you can't expect it to work from the C# code. Permissions issue? You aren't trying to modify the files on a readonly drive (for instance, a mounted iso) ? – Kevin Gosse May 23 '12 at 08:13
  • @KooKiz I don't think it is a permissons issue, because after the exception is thrown that access is denied if I go into subfolder and chnage the permissons of each file in there I can do that and then copy them. But what I want to do is to do it all straight after the extraction. – ELSheepO May 23 '12 at 08:32
  • So now we know that a 'Access is denied' exception is thrown. Please give all the information beforehand. Exactly which line of your code triggers the exception? – Kevin Gosse May 23 '12 at 08:38
  • Hey I think your files are Readonly beforehand as I created a short iso and checked out that no files were readonly after extraction...So this is not the fault of the ISO Extractor code...So you should use some other code[maybe from the answers] to set the files writable! – Writwick May 23 '12 at 08:44
  • sorry I didn't include it, just slipped my mind. `Access to the path 'C:\Documents and Settings\user\My Documents\ISO DUMP!!\extracted ISO\Test\subtetst' is denied.` is the exception thats thrown. If I go outside the IDE and right click, copy and then paste in my documents it works, so I do have access. Have tried Steve's answer, it didn't work, am now running Niranjan Kala's answer – ELSheepO May 23 '12 at 11:02
  • On which line did you get the exception? Directory.GetFiles or File.Copy? – Steve May 23 '12 at 12:23

2 Answers2

1

You can try this to change the attributes:

foreach (string fileName in System.IO.Directory.GetFiles(path))
{
    System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName);

    fileInfo.Attributes |= System.IO.FileAttributes.ReadOnly;
    // or
    fileInfo.IsReadOnly = true;
}

You can try this to recursively set the attribute to all sub directories and files :

DirectoryInfo di = new DirectoryInfo(directorypath);
public void Recurse(DirectoryInfo directory)
{
    foreach (FileInfo fi in directory.GetFiles())
    {
        fi.IsReadOnly = false; // or true
    }

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

Test if user has write access to a folder- Check it, If there is still problem then I believe solution should be to use Windows Shell API

Community
  • 1
  • 1
Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
1

Try to use this code

   fileInfo.IsReadOnly = False

instead of

fileInfo.Attributes = FileAttributes.Normal

Something is wrong in your code above: these lines, given the path in one of your comments

"C:\Documents and Settings\user\My Documents\ISO DUMP!!\extracted ISO\Test\subtetst"
                                                                           ^^^^^^^^
string folderTestTo = folderName[folderName.Length - 1];            
folderTestTo = folderTestTo.Remove(folderTestTo.Length - 4, 4);            
string copyTestTo = folderTestTo;    

will give back this value in copyTestTo:

subt

then you do

string destinatationPath = Path.Combine(copyTestTo, 
                                        Path.GetFileName(copyTestFrom));

and this will give back something impossible as path like this

 "subt\C:\Documents and Settings\user\My Documents\ISO DUMP!!\extracted ISO\Test\subtetst"

I think you should check carefully these passages in your code setting some breakpoint there and examining the values of your vars

Steve
  • 213,761
  • 22
  • 232
  • 286
  • My bad. IsReadOnly=False not True – Steve May 23 '12 at 12:20
  • Could you show me the values of `txtCopyFrom.Text` and `txtCopyTo.Text` before the start of your processing. These two values are the start of everything – Steve May 24 '12 at 13:39
  • txtCopyto.Text `"C:\\Documents and Settings\\user\\My Documents\\ISO DUMP!!\\extracted iso"` and txtCopyFrom is just the server location, I use that to get the iso name and remove the .iso extrention to name the folder – ELSheepO May 24 '12 at 13:46