从C:\ root目录开始迭代文件系统?

时间:2012-01-30 19:03:32

标签: c# filesystems iterator iteration loops

我非常接近完成这个项目但是我有一个我无法弄清楚的问题。 例如,如果我运行我的程序并在“我的文档”中启动迭代。一切都很完美。程序迭代,将结果写入csv文件,就像告诉它一样。但是,如果我在C:\开始迭代(你会在下面看到我写的“捕获”一个UnauthorizedAccessException)我编码的消息弹出告诉我,我没有权限访问任何一个目录,它甚至没有创建csv文件。这是我的代码,任何帮助都会很棒。

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 个答案:

答案 0 :(得分:3)

如果您在Windows 7或Vista上运行此操作,则无法在没有出现该消息的情况下写入许多目录,从而提示您使用管理员权限运行该应用程序。

要查看这是否是您遇到的问题,请以管理员身份启动Visual Studio(在开始菜单中右键单击VS并选择“以管理员身份运行”)。然后,通过Visual Studio打开您的项目并运行它。如果它运行并创建了CSV文件,那么缺少提升权限就成了问题。如果仍然出现错误消息,那么您知道它是其他内容。

(我建议不要测试“C:\中的所有内容” - 在程序文件中创建一个目录,并将其用作测试此问题的沙箱)。