public static void RecursiveSetAttributes(string path, FileAttributeSetArgument attr) { string realPath = PathExpress.ResolvePath(path); string[] dirPaths = Directory.GetDirectories(realPath); string[] filePaths = Directory.GetFiles(realPath); try { SetAttribute(realPath, attr); } catch (Exception ex) { TfxLogger.Error(DefaultLogTag.System, s_instance, "RecursiveSetAttributes", string.Empty, ex); } foreach (string dirPath in dirPaths) { RecursiveSetAttributes(dirPath, attr); } foreach (string filePath in filePaths) { try { FileExpress.SetAttribute(filePath, attr); } catch (Exception ex) { TfxLogger.Error(DefaultLogTag.System, s_instance, "RecursiveSetAttributes", string.Empty, ex); } } }
public static void SetAttribute(string path, FileAttributeSetArgument attr) { string realPath = PathExpress.ResolvePath(path); DirectoryInfo dirInfo = new DirectoryInfo(realPath); FileAttributes dirAttrs = dirInfo.Attributes; if (attr.Archive.HasValue) { dirAttrs = attr.Archive == true ? dirAttrs | FileAttributes.Archive : dirAttrs & ~FileAttributes.Archive; } if (attr.Hidden.HasValue) { dirAttrs = attr.Hidden == true ? dirAttrs | FileAttributes.Hidden : dirAttrs & ~FileAttributes.Hidden; } if (attr.ReadOnly.HasValue) { dirAttrs = attr.ReadOnly == true ? dirAttrs | FileAttributes.ReadOnly : dirAttrs & ~FileAttributes.ReadOnly; } if (attr.System.HasValue) { dirAttrs = attr.System == true ? dirAttrs | FileAttributes.System : dirAttrs & ~FileAttributes.System; } dirInfo.Attributes = dirAttrs; if (dirInfo.Attributes.HasFlag(FileAttributes.ReadOnly)) { return; } if (attr.CreatedTime.HasValue) { dirInfo.CreationTime = attr.CreatedTime.Value; } if (attr.AccessedTime.HasValue) { dirInfo.LastAccessTime = attr.AccessedTime.Value; } if (attr.ModifiedTime.HasValue) { dirInfo.LastWriteTime = attr.ModifiedTime.Value; } }