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

Writing out names of files on local C drive to a text document

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

Problem

This program writes out all the files in the current directory level to a text file, outputs an array of sub-directories at the current level, then repeats this process until there are no more sub-directories to search. The code below take 50 seconds to run.

I was hoping I could get any pointers on the code in general (I'm very new to C#) and also any suggestions for improving the runtime.

```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ComputerFileOutput
{
class Program
{
// Prints a string array of files located in specified path
static void filesArray(string path)
{
string[] array = Directory.GetFiles(path);
addFilesToTextFile(array);
}

//Appends files to .txt document
static void addFilesToTextFile(string[] fileArray)
{
//Append fileArray to current .txt document
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\ComputerFiles.txt", true))
{
for (int i = 0; i subDirectories = new List();
foreach(string directoryName in directoryArray)
{
try
{
filesArray(directoryName);

subDirectories.AddRange(getDirectories(directoryName));
}
catch(Exception)
{
Console.WriteLine("Unathorized to access directory: {0}", directoryName);
}
}

directoryArray = subDirectories.ToArray();

if (directoryArray.Count() == 0)
{
moreDirectories = false;
break;
}
}

// wait for input before exiting
DateTime t2 = DateTime.Now;
Console.WriteLine("Total seconds = {0}", (t2

Solution

First of all, you shouldn't hardcode values like your C:\ or C:\ComputerFiles.txt. If you want to change this, you don't want to edit it at multiple places.

static void filesArray(string path)


The usual .Net naming convention for methods is PascalCase (you'll notice that Main has to follow this convention).

Also, filesArray is pretty bad name for this method. It doesn't tell you what the method does (that it prints somethings). On the other hand, it tells you about an implementation detail (it uses an array), which it shouldn't.

static void addFilesToTextFile(string[] fileArray)


Whenever you call this method, you open and then close a file. I'm not sure about this, but it could add a measurable overhead, considering that you're going to have huge amount of directories.

return (Directory.GetDirectories(path));


The outer parentheses are not necessary here, remove them.

static void Main(string[] args)


If you're not going to use command-line arguments, you can safely remove the args parameter.

DateTime t1 = DateTime.Now;


To measure runtime of your program accurately, you should usually use Stopwatch instead of DateTime. Though if the time is on the order of 50 s, you don't need millisecond accuracy, so DateTime is probably okay.

while (moreDirectories == true)


This whole loop could be simplified if you used Queue. Something like:

//Get array of primary directories in C drive
var directories = new Queue(getDirectories("C:\\"));

//Go through each directory
while (directories.Count > 0)
{
    string directoryName = directories.Dequeue();

    try
    {
        filesArray(directoryName);

        foreach (var subdirectory in getDirectories(directoryName))
            directories.Enqueue(subdirectory);
    }
    catch (Exception)
    {
        Console.WriteLine("Unathorized to access directory: {0}", directoryName);
    }
}


If you don't like that foreach, you could easily create an extension method for it, called something like EnqueueRange().

catch(Exception)


If you're expecting unauthorized access errors, then you should catch UnauthorizedAccessException, nothing else.

Code Snippets

static void filesArray(string path)
static void addFilesToTextFile(string[] fileArray)
return (Directory.GetDirectories(path));
static void Main(string[] args)
DateTime t1 = DateTime.Now;

Context

StackExchange Code Review Q#30313, answer score: 3

Revisions (0)

No revisions yet.