Extracted Files are always read-only

巧了我就是萌 提交于 2020-01-16 18:20:23

问题


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


回答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




回答2:


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



来源:https://stackoverflow.com/questions/10715768/extracted-files-are-always-read-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!