Hi NG !
In my first post you requested me to post some sample code.
If I copy a file A (readonly and hidden flag set) and a file B (only
archive flag set) with this code. It would work. Now I would like to do
the same once again. It works because I remove the readonly flag on the
targetFile. But if i set the hidden flag on the targetFile B in the
explorer and run this code once again, an exception would be thrown.
Why ? File A has set the hidden flag to, and the copy process works.
Why not when I change this manually ?
FileInfo backupFile = new FileInfo(sSourceFile);
FileInfo targetFile = new FileInfo(sTargetPath);
//create the target sbudirectory if it does not exist and
copy the file
if (targetFile.Directory.Exists == false)
{targetFile.Directory.Create();}
//Do not copy security backup files, which would not be
deleted by the user.
if
(backupFile.Extension.Equals(sm_sRestoreSecurityExtension) == false) {
//Ensure that no read only flag is set
if (targetFile.Exists == true) {
targetFile.Attributes = targetFile.Attributes ^
FileAttributes.Readonly;
}
//If the readonly flag (in the targetFile) would be set
manually, this command would result in a exception. But why ? Above I
remove this flag!
backupFile.CopyTo(targetFile.FullName, true);
targetFile.Refresh();
FileAttributes targetFileAtt = targetFile.Attributes;
targetFileAtt = targetFileAtt |
FileAttributes.ReadOnly;
targetFile.Attributes = targetFileAtt;
}
|