Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7185099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:20:25+00:00 2026-05-28T18:20:25+00:00

I am very close to finishing this project however I have a problem that

  • 0

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);
            }
        }

    }
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:20:26+00:00Added an answer on May 28, 2026 at 6:20 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very close and have this working in Safari, Firefox and IE8, however IE7
We have a game that is very close to being released, but we have
I have close to 30 wordpress installs that will grow very rapidly to double
I have made a very simple AppleScript to close tabs in Safari. The problem
I'm very close to figuring this out, but can't seem to find the missing
I'm very close to being done, but can't quite figure out how to tie
Preamble: This is very close to this question , and I have read this
I have come very close to finding a solution to this one; just missing
There have been a couple of questions very close to this topic, but none
I have some code that has got me very close to what Im trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.