I am very close to finishing this project however I have a problem that I cannot figure out.
If I run my program and start the iteration in My Documents for example. Everything works perfectly. The program iterates, writes the results to a csv file just like a tell it to. However, if I start the iteration at C:\ (you’ll see below I’ve written to “catch” an UnauthorizedAccessException) the messages I’ve coded pop up telling me that I don’t have permission to access any of the directories and it doesn’t even create the csv file. Here is my code any help would be great.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FileIterator
{
class Iterator
{
public class MyItem
{
public static string it { get; set; }
}
public class Record
{
public long fileSize { get; set; }
public string fileName { get; set; }
}
static List<Record> fileList = new List<Record>();
static string longest = " ";
static string shortest = " ";
public static void Iterate(string dir_tree)
{
Stack<string> dirs = new Stack<string>(20);
if (!Directory.Exists(dir_tree))
{
MessageBox.Show("The directory you selected does not exist.", "Directory Selection Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
dirs.Push(dir_tree);
while (dirs.Count > 0)
{
string currentDir = dirs.Pop();
string[] subDirs;
try
{
subDirs = Directory.GetDirectories(currentDir);
}
catch (UnauthorizedAccessException)
{
MessageBox.Show("You do not have permission to access this folder " + currentDir, "Directory Permission Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
continue;
}
catch (DirectoryNotFoundException)
{
MessageBox.Show("The current directory does not exist", "Directory Not Found",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
continue;
}
string[] files = null;
try
{
files = System.IO.Directory.GetFiles(currentDir);
}
catch (UnauthorizedAccessException)
{
MessageBox.Show("You do not have permission to access this folder " + currentDir, "Directory Permission Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
continue;
}
catch (DirectoryNotFoundException)
{
MessageBox.Show("The current directory does not exist", "Directory Not Found",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
continue;
}
foreach (string file in files)
{
try
{
FileInfo fi = new FileInfo(file);
fileList.Add( new Record {
fileName = fi.Name,
fileSize = fi.Length
});
}
catch (FileNotFoundException)
{
MessageBox.Show("The current file does not exist" + file, "File Not Found",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
continue;
}
}
foreach (string str in subDirs)
dirs.Push(str);
}
using (var writer = new StreamWriter(@"C:\files.csv"))
{
writer.WriteLine("Name,Size"); // Header
var query = fileList.OrderBy(r => r.fileName);
foreach (Record record in query)
{
writer.WriteLine("\"{0}\",{1}", record.fileName, record.fileSize);
}
}
}
}
}
If you’re running this on Windows 7 or Vista, you don’t get permissions to write to a lot of directories without that message coming up prompting you to run the application with administrator permissions.
To see if this is the issue that you’re having, start Visual Studio as administrator (right click on VS in the start menu and select “run as administrator”). Then, open your project through Visual Studio and run it. If it runs and creates your CSV files, then your lack of elevated permissions was the problem. If the error message still occurs, then you know that it’s something else.
(I would recommend not testing on “everything in C:\” though — create a directory in program files and use that as your sandbox for testing this issue).