建议将String结果转换为Boolean结果. [英] Advice converting String result to Boolean result.

查看:92
本文介绍了建议将String结果转换为Boolean结果.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

在下面的代码(请参见下面的代码)中,我试图将函数"public static string GetDirectoryAuditControlInformation(string path)"转换为布尔函数.

而不是"return info.ToString();",我想返回TRUE是String aceinfo包含信息,如果String aceinfo为空(意味着路径包含零个SACL信息),则返回FALSE.
我知道该功能需要更改为公共静态布尔,而不是静态字符串.在我的尝试中,我只是没有获得所需的结果.

如果有人可以为此提供一些帮助,我将不胜感激.

谢谢你!

Hello,

In the following code (see code below) I am attempting to convert the function " public static string GetDirectoryAuditControlInformation(string path)" into a Boolean Function.

Instead of, "return info.ToString();", I would like return TRUE is String aceinfo contains information and FALSE if String aceinfo is empty (Meaning the path contained zero SACL information.)

I know that the Function needs to be changed to public static bool, instead of static string. In my attempts I am just not getting the results I need.

If anyone could lend some assistance to this I would greatly appreciate it.

Thank You!

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();  
  }

推荐答案

类似的东西:
Something like:
public static bool GetDirectoryAuditControlInformation(string path)
{
  bool result = false;  // assume no SACL info
  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);
   if (aceinfo != null)
   {
     // found SACL info so set result .TRUE.
     result = true;
     info.AppendLine(aceInfo);
   }
  }
 return result;
 }


这篇关于建议将String结果转换为Boolean结果.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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