HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpModerate

Get directory permissions for all directories in tree

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
directorypermissionsallgetfortreedirectories

Problem

I have made a program that given a directory, will get the permissions for that directory and all sub-directories. The output is stored in a string that can be easily written to .csv. Each permissions entry is given its own line. The path is displayed first, with each directory level split into separate cells. Obviously, if the directory nesting has varying depth, the rows have a different number of cells. The permissions data follows, aligned after the deepest level, so the output would look like this:

Right now, the only way I see to do this is a multiple iteration of the results. I must go through the file system to fetch all the directories and save them, then find the longest path, and then format them all into a string. Needless to say, this takes a very long time on large file systems, such as one of my company's network shares with over 200,000 directories. It takes over a day for the program to run on this share, but if I make the path print in a single cell, using only one iteration, it takes minutes.

For more narrow queries, the program runs acceptably well, usually within a few seconds. However, I'm sure there is some way to reduce the multiple iterations so that the time required is reduced also.

PermissionsChecker

```
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Extensions.CollectionExtensions;

namespace DirectoryPermissionsChecker
{
internal class PermissionsChecker
{
public static readonly string OutputPath =
Path.Combine(
Environment.GetFolderPath(
Environment.SpecialFolder.MyDocuments),
"DirectoryPermissionsChecker");

private const string LogFileName = "Log";
private const string OutputFileName = "DirectoryPermissions";

private const string CsvExtension = ".csv";
private const string DateTimeFormat = "yyyyMMddTHHmmss";

private readonly Cancella

Solution

This is not efficient

pathString +=


String is immutable so it builds a new string every time

Use StringBuilder

Code Snippets

pathString +=

Context

StackExchange Code Review Q#135982, answer score: 10

Revisions (0)

No revisions yet.