public void MakeXml(PropertyInfo fileInfo)
 {
     try
     {
         GetDirectoryXml(_doc, fileInfo);
     }
     catch (Exception ex)
     {
         SaveExc.Save(ex);
     }
 }
 private void GetDirectoryXml(XElement elem, PropertyInfo fileInfo)
 {
     var xName = "dir";
     XElement find = elem;
     var nameDirs = fileInfo.FullName.Remove(0, _pathLength).TrimStart('\\', '/').Split('\\', '/');
     if (string.IsNullOrEmpty(nameDirs[0]))
     {
         // Игнорируем дерикторию укзанную для перебора поддиректорий
         return;
     }
     
     for (var i = 0; i < nameDirs.Length; i++)
     {
         // Полное имя файла "FullName" состоит из последовательности директорий и последним идет имя файла (dir\dir\dir\file)
         if (i == nameDirs.Length - 1 && fileInfo.IsFile)
         {
             xName = "file";
         }
         var name = nameDirs[i];
         IEnumerable<XElement> findElements =
             from el in find.Descendants(xName)
             where (string)el.Attribute("name") == name
             select el;
         
         // Если узел с заданными параметрами не существует - создаем
         if (!findElements.Any())
         {
             find.Add(new XElement(xName
                 , new XAttribute("name", fileInfo.Name)
                 , new XAttribute("creationtime", fileInfo.CreationTime)
                 , new XAttribute("lastaccesstime", fileInfo.LastAccessTime)
                 , new XAttribute("lastwritetime", fileInfo.LastWriteTime)
                 ));
         }
         else
         {
             find = findElements.First();
         }
     }
 }
Ejemplo n.º 3
0
 private void FillSubNodes(TreeNodeCollection nodes, PropertyInfo fileInfo)
 {
     // Удаляем из полного пути путь к указанной директории для перебора поддиректорий
     var nameDirs = fileInfo.FullName.Remove(0, _workDirLength).TrimStart('\\', '/').Split('\\', '/');
     if (string.IsNullOrEmpty(nameDirs[0]))
     {
         // Игнорируем дерикторию указанную для перебора поддиректорий
         return;
     }
     foreach (var name in nameDirs)
     {
         // Если в коллекции узлов нет указанного узла, то добавляем
         if (!nodes.ContainsKey(name))
         {
             var indexImg = fileInfo.IsFile ? 2 : 0;
             var indexSelectImg = fileInfo.IsFile ? 3 : 1;
             TreeNode node = new TreeNode(name, indexImg, indexSelectImg)
             {
                 Name = name,
                 Tag = fileInfo
             };
             
             nodes.Add(node);
         }
         else // иначе - переходим на дочерний уровень
         {
             nodes = nodes[name].Nodes;
         }
     }
 }
 private void GetAccessRules(WindowsIdentity wi, FileSystemAccessRule fsAccessRule, FileSystemRights fsRight, PropertyInfo file)
 {
     SecurityIdentifier sid = (SecurityIdentifier)fsAccessRule.IdentityReference;
     if (((fsAccessRule.FileSystemRights & fsRight) == fsRight))
     {
         if (wi != null &&
             ((sid.IsAccountSid() && wi.User == sid)
             || (wi.Groups != null && (!sid.IsAccountSid() && wi.Groups.Contains(sid)))))
         {
             var isAllow = fsAccessRule.AccessControlType == AccessControlType.Allow;
             switch (fsRight)
             {
                 case FileSystemRights.FullControl:
                     file.FullControl = isAllow;
                     break;
                 case FileSystemRights.Modify:
                     file.Modify = isAllow;
                     break;
                 case FileSystemRights.Read:
                     file.Read = isAllow;
                     break;
                 case FileSystemRights.ReadAndExecute:
                     file.ReadAndExecute = isAllow;
                     break;
                 case FileSystemRights.Write:
                     file.Write = isAllow;
                     break;
             }
         }
     }
 }
 private void GetAccessRules(FileSystemSecurity fsSecurity, PropertyInfo file)
 {
     try
     {
         var getOwner = fsSecurity.GetOwner(typeof (SecurityIdentifier));
         if (getOwner != null)
         {
             string ownerIdentifier = fsSecurity.GetOwner(typeof(SecurityIdentifier)).Value;
             //var owner = fsSecurity.GetOwner(typeof(SecurityIdentifier));
             //var nameOwner = owner.Translate(typeof(NTAccount)).Value;
             file.Owner = ownerIdentifier;
         }
     }
     catch(Exception ex){
          /* 
            System.Security.Principal.IdentityNotMappedException was unhandled
            Message="Some or all identity references could not be translated."
          */
         SaveExc.Save(ex);
     }
     WindowsIdentity wi = WindowsIdentity.GetCurrent();
     AuthorizationRuleCollection rules = fsSecurity.GetAccessRules(true, true, typeof(SecurityIdentifier));
     foreach (FileSystemAccessRule rl in rules)
     {
         GetAccessRules(wi, rl, FileSystemRights.FullControl, file);
         GetAccessRules(wi, rl, FileSystemRights.Modify, file);
         GetAccessRules(wi, rl, FileSystemRights.Read, file);
         GetAccessRules(wi, rl, FileSystemRights.ReadAndExecute, file);
         GetAccessRules(wi, rl, FileSystemRights.Write, file);
     }
 }
 private PropertyInfo FillPropertyInfo(bool isFile, FileSystemInfo info)
 {
     var file = new PropertyInfo
     {
         IsFile = isFile,
         Name = info.Name,
         FullName = info.FullName,
         CreationTime = info.CreationTime,
         LastAccessTime = info.LastAccessTime,
         LastWriteTime = info.LastWriteTime,
         ReadOnly = false,
         Hidden = false,
         Archive = false,
         NotContentIndexed = false,
         Compressed = false,
         Encrypted = false,
         Owner = string.Empty
     };
     FileAttributes attr = info.Attributes;
     if ((attr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
     {
         file.ReadOnly = true;
     }
     if ((attr & FileAttributes.Hidden) == FileAttributes.Hidden)
     {
         file.Hidden = true;
     }
     if ((attr & FileAttributes.Archive) == FileAttributes.Archive)
     {
         file.Archive = true;
     }
     if ((attr & FileAttributes.NotContentIndexed) == FileAttributes.NotContentIndexed)
     {
         file.NotContentIndexed = true;
     }
     if ((attr & FileAttributes.Compressed) == FileAttributes.Compressed)
     {
         file.Compressed = true;
     }
     if ((attr & FileAttributes.Encrypted) == FileAttributes.Encrypted)
     {
         file.Encrypted = true;
     }
     return file;
 }