gpt4 book ai didi

C# EnumerateDirectories 不应使用本地化路径

转载 作者:行者123 更新时间:2023-12-04 13:34:28 24 4
gpt4 key购买 nike

下面的代码产生以下输出:C:\ProgramData\Microsoft\Windows\Start Menu\ProgramsC:\ProgramData\Microsoft\Windows\Start Menu\Programme (本地化德语)
问题 :这些都指的是同一个目录,但我不能在本地化路径上调用 EnumerateFiles(这无论如何都没有意义),因为它会产生 System.UnauthorizedAccessException,这也意味着我不能将 EnumerateFiles 与 SearchOption.AllDirectories 一起使用。因为我得到了同样的异常(exception)。
有没有办法只从 EnumerateDirectories 中获取功能性非本地化路径?

//Root Directory is C:\ProgramData\Microsoft\Windows\Start Menu
foreach(DirectoryInfo directoryInfo in rootDirectory.EnumerateDirectories("*", SearchOption.TopDirectoryOnly))
{
Console.WriteLine("Dir: " + directoryInfo.FullName);
//C:\ProgramData\Microsoft\Windows\Start Menu\Programme
//C:\ProgramData\Microsoft\Windows\Start Menu\Programs
foreach (FileInfo fileInfo in directoryInfo.EnumerateFiles("*", SearchOption.TopDirectoryOnly))
{
//do stuff
}
}

最佳答案

您必须递归遍历每个目录,以便在您无权访问文件夹时添加异常处理程序。下面是代码的一个例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace SAveDirectoriesXml
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
const string FOLDER = @"c:\temp";
static XmlWriter writer = null;
static void Main(string[] args)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;

writer = XmlWriter.Create(FILENAME, settings);
writer.WriteStartDocument(true);

DirectoryInfo info = new DirectoryInfo(FOLDER);
WriteTree(info);

writer.WriteEndDocument();
writer.Flush();
writer.Close();
Console.WriteLine("Enter Return");
Console.ReadLine();

}
static long WriteTree(DirectoryInfo info)
{
long size = 0;
writer.WriteStartElement("Folder");
try
{
writer.WriteAttributeString("name", info.Name);
writer.WriteAttributeString("numberSubFolders", info.GetDirectories().Count().ToString());
writer.WriteAttributeString("numberFiles", info.GetFiles().Count().ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());


foreach (DirectoryInfo childInfo in info.GetDirectories())
{
size += WriteTree(childInfo);
}

}
catch (Exception ex)
{
string errorMsg = string.Format("Exception Folder : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}

FileInfo[] fileInfo = null;
try
{
fileInfo = info.GetFiles();
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception FileInfo : {0}, Error : {1}", info.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error",errorMsg);
}

if (fileInfo != null)
{
foreach (FileInfo finfo in fileInfo)
{
try
{
writer.WriteStartElement("File");
writer.WriteAttributeString("name", finfo.Name);
writer.WriteAttributeString("size", finfo.Length.ToString());
writer.WriteAttributeString("date", info.LastWriteTime.ToString());
writer.WriteEndElement();
size += finfo.Length;
}
catch (Exception ex)
{
string errorMsg = string.Format("Exception File : {0}, Error : {1}", finfo.FullName, ex.Message);
Console.WriteLine(errorMsg);
writer.WriteElementString("Error", errorMsg);
}
}
}

writer.WriteElementString("size", size.ToString());
writer.WriteEndElement();
return size;

}
}
}

关于C# EnumerateDirectories 不应使用本地化路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63012721/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com