[英]c# getting files form directory
i have problem with my c# code, i want to list all files from mozilla user data folder, i was trying to do it with: 我的C#代码有问题,我想列出mozilla用户数据文件夹中的所有文件,我试图这样做:
String dir = Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Mozilla\\Firefox\\Profiles\\")[0];
string[] fileEntries = Directory.GetFiles(Environment.GetEnvironmentVariable("AppData") + "\\Mozilla\\Firefox\\Profiles\\" + dir);
But when i execute this code i get error that tells me that format of this path is not supported. 但是,当我执行此代码时,我得到一个错误,告诉我该路径的格式不受支持。 In error info i can see that my path is: 在错误信息中,我可以看到我的路径是:
"C:\\Users\\Marcin\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\4wrivbme.default" “C:\\用户\\马尔钦\\应用程序数据\\漫游\\ Mozilla的\\火狐\\概况\\ 4wrivbme.default”
Its because of \\? 是因为\\? I'm sure that i need this. 我确定我需要这个。
If I have understood correctly your code, then the second line should be just 如果我正确理解了您的代码,那么第二行应该是
string[] fileEntries = Directory.GetFiles(dir);
On my PC your second line gives back this path that is clearly wrong 在我的PC上,您的第二行会返回该路径,这显然是错误的
C:\Users\steve\AppData\Roaming\Mozilla\Firefox\Profiles\C:\Users\steve\AppData.....
^^^^^
So a full fix of your code should be as this 因此,您的代码的完整修复应为:
string mozillaProfilePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"Mozilla\\Firefox\\Profiles");
string[] dirs = Directory.GetDirectories(mozillaProfilePath);
if(dirs.Length > 0)
{
string[] fileEntries = Directory.GetFiles(dirs[0]);
......
}
and, if your process the result directly here, then it is better to use Directory.EnumeratFiles instead of GetFiles (See remarks in the link posted) 并且,如果您直接在此处处理结果,则最好使用Directory.EnumeratFiles而不是GetFiles(请参阅发布的链接中的备注)
.....
if(dirs.Length > 0)
{
foreach(string file in Directory.EnumerateFiles(dirs[0]))
{
.... process each file found ...
}
}
使用Path.Combine()代替字符串串联。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.