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

C# 从C:\root目录开始迭代文件系统?,c#,filesystems,iterator,iteration,loops,C#,Filesystems,Iterator,Iteration,Loops,我非常接近完成这个项目,但我有一个问题,我不能弄清楚。 例如,如果我运行我的程序并在文档中开始迭代。一切都很完美。程序迭代,将结果写入csv文件,就像告诉它一样。但是,如果我从C:\(你会看到下面我写的是为了“捕获”一个未授权的访问异常)开始迭代,我编写的消息会弹出,告诉我我没有访问任何目录的权限,它甚至不会创建csv文件。这是我的代码,任何帮助都会很好 using System; using System.Collections.Generic; using System.ComponentM

我非常接近完成这个项目,但我有一个问题,我不能弄清楚。 例如,如果我运行我的程序并在文档中开始迭代。一切都很完美。程序迭代,将结果写入csv文件,就像告诉它一样。但是,如果我从C:\(你会看到下面我写的是为了“捕获”一个未授权的访问异常)开始迭代,我编写的消息会弹出,告诉我我没有访问任何目录的权限,它甚至不会创建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);
            }
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
使用System.IO;
命名空间文件迭代器
{
类迭代器
{
公共类MyItem
{
公共静态字符串it{get;set;}
}
公开课记录
{
公共长文件大小{get;set;}
公共字符串文件名{get;set;}
}
静态列表fileList=新列表();
静态字符串最长=”;
静态字符串最短=”;
公共静态无效迭代(字符串目录树)
{
堆栈dirs=新堆栈(20);
如果(!Directory.Exists(dir_tree))
{
MessageBox.Show(“您选择的目录不存在。”,“目录选择错误”,
MessageBoxButtons.OK,MessageBoxIcon.感叹号);
}
dirs.Push(dir_树);
而(dirs.Count>0)
{
字符串currentDir=dirs.Pop();
字符串[]子字段;
尝试
{
subDirs=Directory.GetDirectories(currentDir);
}
捕获(未经授权的访问例外)
{
MessageBox.Show(“您没有访问此文件夹的权限”+currentDir,“目录权限错误”,
MessageBoxButtons.OK,MessageBoxIcon.感叹号);
继续;
}
捕获(DirectoryNotFoundException)
{
MessageBox.Show(“当前目录不存在”,“未找到目录”,
MessageBoxButtons.OK,MessageBoxIcon.感叹号);
继续;
}
string[]files=null;
尝试
{
files=System.IO.Directory.GetFiles(currentDir);
}
捕获(未经授权的访问例外)
{
MessageBox.Show(“您没有访问此文件夹的权限”+currentDir,“目录权限错误”,
MessageBoxButtons.OK,MessageBoxIcon.感叹号);
继续;
}
捕获(DirectoryNotFoundException)
{
MessageBox.Show(“当前目录不存在”,“未找到目录”,
MessageBoxButtons.OK,MessageBoxIcon.感叹号);
继续;
}
foreach(文件中的字符串文件)
{
尝试
{
FileInfo fi=新的FileInfo(文件);
添加(新记录){
fileName=fi.Name,
fileSize=fi.Length
});
}
捕获(FileNotFoundException)
{
MessageBox.Show(“当前文件不存在”+文件,“未找到文件”,
MessageBoxButtons.OK,MessageBoxIcon.感叹号);
继续;
}
}
foreach(子目录中的字符串str)
直接推力(str);
}
使用(var writer=newstreamwriter(@“C:\files.csv”))
{
writer.WriteLine(“名称、大小”);//标题
var query=fileList.OrderBy(r=>r.fileName);
foreach(查询中的记录)
{
writer.WriteLine(“{0}\”,{1}”,record.fileName,record.fileSize);
}
}
}
}

}

如果您在Windows 7或Vista上运行此应用程序,如果没有提示您使用管理员权限运行应用程序的消息,您将无法获得写入许多目录的权限

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


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

运行该应用程序的用户有哪些权限?@Joe这是个好问题,因为我是C中的noob,我不知道。我怎么检查?你是这台机器的管理员吗?您是从您的帐户运行它吗?