需要帮助以获取返回的目录,子目录和文件Sacl信息. [英] Need Help getting Directory, SubDirectory and File Sacl information returned.

查看:77
本文介绍了需要帮助以获取返回的目录,子目录和文件Sacl信息.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我开发了带有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();
  }

推荐答案



您可以使用递归遍历所有子文件夹.例如:
-使用递归扫描目录 [ http://support.microsoft.com/kb/303974 [
Hi,

You could use recursion to iterate through all subfolders. For example:
- Scan directories using recursion[^]
- http://support.microsoft.com/kb/303974[^]


您无需执行递归就可以了,因为已经提供了递归搜索使用可选参数System.IO.SearchOption.AllDirectories:

You can go without implementing recursion, because recursive search is already provided using the optional parameter System.IO.SearchOption.AllDirectories:

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.

—SA



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.

—SA


好的,我仍然在删除目录,子目录和文件的所有Sacl信息时遇到问题.我尝试了递归,但它仍然只是删除目录.另外,我尝试使用Directory.GetFiles(3个参数),不喜欢searchpattern.阅读所附的讨论,但似乎没有解决问题的方法.
Okay, I am still having issues removing all the Sacl information for Directory, SubDirectory, and files. I tried the recursion and it still just removes the Directory. Also, I attempted to use the Directory.GetFiles( 3 parameters), doesn''t like the searchpattern. Read the discussion attached but didn''t seem to do the trick.


这篇关于需要帮助以获取返回的目录,子目录和文件Sacl信息.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆