Issue with permissions, works Win7, but not XP

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have the following code that works fine on Windows 7, but fails to work on Windows XP.
The code runs fine for awhile, then when it tries an overwrite on XP, the read-only attribute is set on the target file. I have this set up to install from a CD (image). As far as I know, after Windows 2000, copying from a CD doesnt leave the RO bit set. Either way, the RO attribute, and permissions for "Everyone" are set to allow deletion using the code below, and it DOES change the permissions and attributes on Win7 just fine.
Does XP require something else, or am I going to end up having to call attrib in order to change the RO attribute?
Currently I have no error trapping since Im trying to figure out just why this isnt working on XP.
Andy #region Make Directory
/// <summary>
/// Create a directory if it doesnt exist
/// </summary>
/// <param name="DirName Pathname of directory</param>
[DebuggerStepThrough]
internal static void MakeDirectory(string DirName)
{
// Does the directory exist?
if (!Directory.Exists(DirName)) {
// Set up security
// Everyone gets full control. Needed for NFS sharing
FileSystemAccessRule EveryoneRule = new FileSystemAccessRule(
"Everyone",
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
// Standard security settings follow
FileSystemAccessRule AuthUsersRule = new FileSystemAccessRule(
"Authenticated Users",
FileSystemRights.Modify |
FileSystemRights.ReadAndExecute |
FileSystemRights.ListDirectory |
FileSystemRights.Read |
FileSystemRights.Write,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
FileSystemAccessRule SystemRule = new FileSystemAccessRule(
"SYSTEM",
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
FileSystemAccessRule AdminRule = new FileSystemAccessRule(
"Administrators",
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
FileSystemAccessRule UserRule = new FileSystemAccessRule(
"Users",
FileSystemRights.ReadAndExecute |
FileSystemRights.ListDirectory |
FileSystemRights.Read,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
// Add the security settings
DirectorySecurity dirSec = new DirectorySecurity();
dirSec.AddAccessRule(EveryoneRule);
dirSec.AddAccessRule(AuthUsersRule);
dirSec.AddAccessRule(SystemRule);
dirSec.AddAccessRule(AdminRule);
dirSec.AddAccessRule(UserRule);
}
}
#endregion
#region Copy All
/// <summary>
/// Copies all files and subdirectories from the source directory to the target
/// </summary>
/// <param name="source Source pathname</param>
/// <param name="target Target pathname</param>
[DebuggerStepThrough]
internal static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
// Check if the directory exists, if not, create it
MakeDirectory(target.FullName);
foreach (FileInfo fi in source.GetFiles()) {
string filename = Path.Combine(target.ToString(), fi.Name);
if (File.Exists(filename)) {
SetSecurity(filename); // Yes, set to allow deletion
FileInfo nf = new FileInfo(filename);
nf.Attributes = FileAttributes.Normal;
nf.IsReadOnly = false;
File.Delete(filename); // Delete the file
}
fi.CopyTo(filename, true);
}
// Copy each directory using recursion
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories()) {
DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
#endregion
#region Add Security
/// <summary>
/// Add "Everyone" to a file or directory
/// </summary>
/// <param name="name </param>
[DebuggerStepThrough]
internal static void SetSecurity(string name)
{
// Get a FileSecurity object that represents the
// current security settings.
DirectorySecurity sec = Directory.GetAccessControl(name);
FileSystemAccessRule EveryoneRule = new FileSystemAccessRule(
"Everyone",
FileSystemRights.FullControl,
InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
PropagationFlags.None,
AccessControlType.Allow);
sec.AddAccessRule(EveryoneRule);
// Set the new access settings.
Directory.SetAccessControl(name, sec);
}
#endregion

View the full article
 
Back
Top