/// <summary>
        /// Ensures that the directory exists
        /// </summary>
        public static void EnsureDirectoryExists(string directory)
        {
            string[] directories = directory.Split(Path.DirectorySeparatorChar);

            if (directories.Length == 2)
            {
                return;
            }

            string currentPath = string.Format("{0}{1}", directories[0], Path.DirectorySeparatorChar);

            for (int i = 1; i < directories.Length; ++i)
            {
                currentPath = string.Format("{0}{1}{2}", currentPath, directories[i], Path.DirectorySeparatorChar);

                if (currentPath.StartsWith(PathUtil.BaseDirectory, StringComparison.InvariantCultureIgnoreCase)) // don't touch dirs outside our own folder!
                {
                    if (C1Directory.Exists(currentPath) == false)
                    {
                        C1Directory.CreateDirectory(currentPath);
                    }
                }
            }
        }
        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);
            }
        }