从 C:\ 根目录开始迭代文件系统?
我非常接近完成这个项目,但是我有一个无法解决的问题。 例如,如果我运行我的程序并在“我的文档”中开始迭代。一切都很完美。程序会迭代,将结果写入 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);
}
}
}
}
}
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);
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在 Windows 7 或 Vista 上运行此应用程序,则如果没有出现该消息提示您使用管理员权限运行该应用程序,您将无法获得写入多个目录的权限。
要查看这是否是您遇到的问题,请以管理员身份启动 Visual Studio(右键单击开始菜单中的 VS,然后选择“以管理员身份运行”)。然后,通过 Visual Studio 打开项目并运行它。如果它运行并创建您的 CSV 文件,那么您缺乏提升的权限就是问题所在。如果错误消息仍然出现,那么您就知道是其他原因了。
(不过,我建议不要对“C:\ 中的所有内容”进行测试——在程序文件中创建一个目录并将其用作测试此问题的沙箱)。
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).