private XmlElement AddNewEntry(EntryInfo entry) { var parentNode = _xmlDocument.CreateElement(Resources.Service_entry); Dictionary<string, string> entryDictionary = new Dictionary<string, string> { {Resources.Service_FullName, entry.FullName}, {Resources.Service_Creation_Time, entry.CreationTime.ToString(CultureInfo.InvariantCulture)}, {Resources.Service_Last_Data_Access_Time, entry.LastDataAccessTime.ToString(CultureInfo.InvariantCulture)}, {Resources.Services_Modification_Time, entry.ModificationTime.ToString(CultureInfo.InvariantCulture)}, {Resources.Services_Size, entry.Size}, {Resources.Services_Owner, entry.Owner}, {Resources.Services_Permissions, entry.Permissions.ToString()} }; try { foreach (var entryElement in entryDictionary) { XmlElement newElement = _xmlDocument.CreateElement(entryElement.Key); newElement.InnerText = entryElement.Value; parentNode.AppendChild(newElement); } } catch (Exception ex) { _helpers.WriteToLog(Resources.Error, ex.Message); } return parentNode; }
public static EntryInfo GetEntryInfo(string path) { FileSystemInfo entry = IsDirectory(path) ? (FileSystemInfo)new DirectoryInfo(path) : new FileInfo(path); var entryInfo = new EntryInfo { Name = entry.Name, FullName = entry.FullName, CreationTime = entry.CreationTime, LastDataAccessTime = entry.LastAccessTime, ModificationTime = entry.LastWriteTime, Owner = IsDirectory(path) ? Directory.GetAccessControl(path).GetOwner(typeof(NTAccount)).ToString() : File.GetAccessControl(path).GetOwner(typeof(NTAccount)).ToString() }; try { FileSystemSecurity security; if (IsDirectory(path)) { var directory = (DirectoryInfo)entry; var directories = directory.GetFiles(); var filesLength = directories.Select(fileInfo => fileInfo.Length); entryInfo.Size = filesLength.Sum().ToString(); security = directory.GetAccessControl(); } else { var file = (FileInfo)entry; entryInfo.Size = file.Length.ToString(); security = file.GetAccessControl(); } foreach (FileSystemAccessRule rule in security.GetAccessRules(true, true, typeof(NTAccount))) { entryInfo.Permissions = rule.FileSystemRights; return entryInfo; } } catch (UnauthorizedAccessException) { entryInfo.Size = Resources.Access_Denided; } catch (Exception ex) { WriteToLog(Resources.Error_message, ex.Message); } return entryInfo; }