问题描述
你好,
我开发了带有CommandArgs的控制台应用程序,该应用程序可以显示并从用户输入的Path中删除SACL.
效果很好.但是,我设置的方式是仅根据输入的目录显示或删除SACLS.
我需要我的代码来枚举目录并不仅显示目录中的信息,还显示目录中的子目录和文件.
任何人都可以使用此代码,或者可以向我指出正确的方向吗?
谢谢你!
这是我所做的一些示例代码.同样,它仅读取目录.我需要目录,子目录和文件.
Hello,
I have developed a Console App with CommandLine Args that can display and then remove SACLs from a user inputted Path.
Works great. However, the way I have it setup is that in only displays or removes the SACLS based on the Directory entered.
I need my code to enumerate the Directory and display information not only from the Directory, but also Subdirectories and Files in the directories.
Anyone have this code available or can point me in the right direction?
Thank You!
Here is some sample code of what I have done. Again, it reads in a Directory only. I need the Directory, SubDirectories, and files.
class Program
{
static void Main(string[] args)
{
try
{
if (args[0].ToUpper().Trim() == "SEARCH") // Displays all Sacls for path
{
Console.WriteLine("Enter your Directory path: ex. C:\\TestFolder");
string path = Console.ReadLine();
Console.WriteLine("{0}", path);
if (Directory.Exists(path))
{
Console.WriteLine(GetDirectoryAuditControlInformation(path));
}
else
{
Console.WriteLine("Path does not exist");
}
}
else if // argument for == CLEAN which removes all Sacls.
// my function for GetDirectoryAuditControlInformation(string path)
public static string GetDirectoryAuditControlInformation(string path)
{
StringBuilder info = new StringBuilder();
info.AppendLine("SACL entries for the path \ "" + path + "\":");
info.AppendLine();
DirectorySecurity dsecurity =
Directory.GetAccessControl(path, AccessControlSections.Audit);
AuthorizationRuleCollection acl =
dsecurity.GetAuditRules(true, true,
typeofSystem.Security.Principal.NTAccount));
foreach (FileSystemAuditRule ace in acl)
{
string aceInfo = GetAuditAceInformation(ace);
info.AppendLine(aceInfo);
}
return info.ToString();
}
public static string GetAuditInformation(FileSystemAuditRule ace)
{
StringBuilder info = new StringBuilder();
string line = string.Format("Account: {0}", ace.IdentityReference.Value);
info.AppendLine(line);
line = string.Format("Type: {0}", ace.AuditFlags);
info.AppendLine(line);
line = string.Format("Rights: {0}", ace.FileSystemRights);
info.AppendLine(line);
line = string.Format("Inherited ACE: {0}", ace.IsInherited);
info.AppendLine(line);
return info.ToString();
}
推荐答案
System.IO.Directory.GetFiles(
path, searchPattern, System.IO.SearchOption.AllDirectories);
System.IO.Directory.GetDirectories(
path, searchPattern, System.IO.SearchOption.AllDirectories);
上面的用法就是您所需要知道的.
但是,有一个警告与searchPattern
参数有关.请参阅以下讨论: Directory.Get.Files搜索模式问题 [ ^ ].一开始,我也犯了一个错误.所有功劳归功于Abhishek Sur和DaveyM69.
The above usage is all you need to know.
There is one caveat though, related to the searchPattern
parameter. Please see this discussion: Directory.Get.Files search pattern problem[^]. At first, I did a mistake, too. All credit goes to Abhishek Sur and DaveyM69.
这篇关于需要帮助以获取返回的目录,子目录和文件Sacl信息.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!