Ejemplo n.º 1
0
        /// <summary>
        /// Delete a file. If the deleteEmptyDirectoresRecursively is true all empty directories
        /// from buttom up are also deleted.
        /// </summary>
        /// <param name="path"></param>
        /// <param name="deleteEmptyDirectoresRecursively"></param>
        public static void DeleteFile(string path, bool deleteEmptyDirectoresRecursively)
        {
            LoggingService.LogVerbose("DirectoryUtil", string.Format("Deleting file '{0}'", path));
            C1File.Delete(path);

            if (deleteEmptyDirectoresRecursively)
            {
                string directory = Path.GetDirectoryName(path);

                string[] directories = directory.Split(Path.DirectorySeparatorChar);

                for (int i = directories.Length; i > 1; --i)
                {
                    StringBuilder stringBuilder = new StringBuilder();

                    for (int j = 0; j < i; ++j)
                    {
                        stringBuilder.Append(directories[j]);
                        stringBuilder.Append(Path.DirectorySeparatorChar);
                    }

                    string currentPath = stringBuilder.ToString();
                    if (C1Directory.GetFiles(currentPath).Length == 0)
                    {
                        LoggingService.LogVerbose("DirectoryUtil", string.Format("Deleting directory '{0}'", currentPath));
                        C1Directory.Delete(currentPath);
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }
Ejemplo n.º 2
0
 /// <exclude />
 public static void Delete(string filePath)
 {
     if (C1File.Exists(filePath))
     {
         RemoveReadOnly(filePath);
         C1File.Delete(filePath);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Deletes all files recursively leaving all sub directories.
        /// </summary>
        /// <param name="directoryPath"></param>
        public static void DeleteFilesRecursively(string directoryPath)
        {
            foreach (string file in C1Directory.GetFiles(directoryPath))
            {
                C1File.Delete(file);
            }

            foreach (string directory in C1Directory.GetDirectories(directoryPath))
            {
                DeleteFilesRecursively(directory);
            }
        }
Ejemplo n.º 4
0
        /// <exclude />
        public static bool RemoveReadOnly(string filePath)
        {
            if (C1File.Exists(filePath) == false)
            {
                return(false);
            }

            FileAttributes fileAttributes = C1File.GetAttributes(filePath);

            if ((fileAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            {
                fileAttributes ^= FileAttributes.ReadOnly;
                C1File.SetAttributes(filePath, fileAttributes);
            }

            return(true);
        }
Ejemplo n.º 5
0
        public static bool WritePermissionGranted(string fileOrDirectoryPath)
        {
            try
            {
                AuthorizationRuleCollection rules;

                if (C1File.Exists(fileOrDirectoryPath))
                {
                    FileSystemSecurity security = File.GetAccessControl(fileOrDirectoryPath);

                    rules = security.GetAccessRules(true, true, typeof(NTAccount));
                }
                else if (C1Directory.Exists(fileOrDirectoryPath))
                {
                    DirectorySecurity security = Directory.GetAccessControl(fileOrDirectoryPath);

                    rules = security.GetAccessRules(true, true, typeof(NTAccount));
                }
                else
                {
                    throw new FileNotFoundException("File or directory '{0}' does not exist".FormatWith(fileOrDirectoryPath));
                }

                var  currentuser = new WindowsPrincipal(WindowsIdentity.GetCurrent());
                bool result      = false;
                foreach (FileSystemAccessRule rule in rules)
                {
                    if ((rule.FileSystemRights & (FileSystemRights.WriteData | FileSystemRights.Write)) == 0)
                    {
                        continue;
                    }

                    if (rule.IdentityReference.Value.StartsWith("S-1-"))
                    {
                        var sid = new SecurityIdentifier(rule.IdentityReference.Value);
                        if (!currentuser.IsInRole(sid))
                        {
                            continue;
                        }
                    }
                    else
                    {
                        if (!currentuser.IsInRole(rule.IdentityReference.Value))
                        {
                            continue;
                        }
                    }

                    if (rule.AccessControlType == AccessControlType.Deny)
                    {
                        return(false);
                    }
                    if (rule.AccessControlType == AccessControlType.Allow)
                    {
                        result = true;
                    }
                }
                return(result);
            }
            catch
            {
                return(false);
            }
        }