Search code examples
c#generic-collectionssystem.io.directory

I am trying to populate a list with properties and strings


I am trying to populate a list with property names and values, here is my code so far...

 public class DirPropeties { 
        public string Name { get; set; }
        public string Path { get; set; }
        public string HiddenStatus { get; set; }
    }
    public static List<string> GetDirAttributes(string path, bool IsHidden = true)
    {

        List<DirPropeties> FolderArrayList = new List<DirPropeties>();
        DirectoryInfo di = new DirectoryInfo(path);
        DirectoryInfo[] subdirectories = di.GetDirectories();
        foreach (DirectoryInfo dir in subdirectories)
        {
            if (IsHidden)
            {
                //FolderArrayList.Add(dir.Name);
                FolderArrayList.Add(new DirPropeties { Name = dir.Name });
            }
            else if ((dir.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
            {
                FolderArrayList.Add(new DirPropeties { Name = dir.Name });
            }

        };

        return FolderArrayList;
    }

I call it like so....

  List<string> DirectoryArrayList = new List<string>();
  DirectoryArrayList = BaseDirectory.GetDirAttributes(FileDetailsViewModel.FolderPath, FileDetailsViewModel.AllowHidden);

and I am trying to access the properties like so ...

   if (folderIndex >= DirectoryArrayList.Count) { break; }
   var folder = DirectoryArrayList[folderIndex];
   var label = new Label()
   {
        Text = folder.Name,
        FontSize = 20,
        VerticalTextAlignment = TextAlignment.Center,
        HorizontalTextAlignment = TextAlignment.Center
   };

I get the following error on the return of the FolderArrayList...

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List'

and on the folder.Name i get the follwoing error...

'string' does not contain a definition for 'Name'

Can anyone help guid me as to what I need to do to solve this please?


Solution

  • There's no need to use List<string> anywhere in this code. Change

    public static List<string> GetDirAttributes(string path, bool IsHidden = true)
    

    to

    public static List<DirPropeties> GetDirAttributes(string path, bool IsHidden = true)
    

    Also change

    List<string> DirectoryArrayList = new List<string>();
    DirectoryArrayList = BaseDirectory.GetDirAttributes(FileDetailsViewModel.FolderPath, FileDetailsViewModel.AllowHidden);
    

    to

    var DirectoryArrayList = BaseDirectory.GetDirAttributes(FileDetailsViewModel.FolderPath, FileDetailsViewModel.AllowHidden);
    

    You can also improve this code

    if (IsHidden)
    {
       //FolderArrayList.Add(dir.Name);
        FolderArrayList.Add(new DirPropeties { Name = dir.Name });
    }
    else if ((dir.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
    {
        FolderArrayList.Add(new DirPropeties { Name = dir.Name });
    }
    

    to

    if (IsHidden || (dir.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden)
    {
        FolderArrayList.Add(new DirPropeties { Name = dir.Name });
    }