none
how to merge DirectoryInfo[] and FileInfo[] type array? RRS feed

  • Question

  • hi all,

    is it possible to merge DirectoryInfo[] and FileInfo[] array so that i can have only one array which hold all information of files and folders in it.

    thanks in advacne :)

    sumitk

    Saturday, January 12, 2013 6:53 AM

Answers

  • Simply do it yourself

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace ConsoleApplication1
    {
        class Program
        {
            public struct DirectoryFileInfo
            {
                public System.Type FileFolder;
                public string DirectoryName;
                public Boolean ISREADONLY;
                public long Length;
                public string Name;
            }
            static void Main(string[] args)
            {
                List<DirectoryFileInfo> filefolders = new List<DirectoryFileInfo>();
                DirectoryInfo dirinfo = new DirectoryInfo(@"c:\temp");
                FileInfo[] files = dirinfo.GetFiles();
                DirectoryInfo[] folders = dirinfo.GetDirectories();
                foreach(FileInfo file in  files)
                {
                    DirectoryFileInfo filefolder = new DirectoryFileInfo();
                    filefolder.FileFolder = file.GetType();
                    filefolder.DirectoryName = file.DirectoryName;
                    filefolder.ISREADONLY = file.IsReadOnly;
                    filefolder.Length = file.Length;
                    filefolder.Name = file.Name;
                    filefolders.Add(filefolder);
                }
                foreach (DirectoryInfo folder in folders)
                {
                    DirectoryFileInfo filefolder = new DirectoryFileInfo();
                    filefolder.FileFolder = folder.GetType();
                    filefolder.DirectoryName = folder.FullName;
                    filefolder.Name = folder.Name;
                    filefolders.Add(filefolder);
                }
            }
        }
    }


    jdweng

    Saturday, January 12, 2013 12:47 PM

All replies

  • DirectoryInfo and FileInfo have a common base class, FileSystemInfo, so you can use FileSystemInfo[].

    If you want to enumerate existing directory and files you may also be interested in the DirectoryInfo.GetFileSystemInfos() method.

    • Proposed as answer by Blackwood Sunday, January 13, 2013 2:23 AM
    Saturday, January 12, 2013 7:07 AM
    Moderator
  • Simply do it yourself

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    namespace ConsoleApplication1
    {
        class Program
        {
            public struct DirectoryFileInfo
            {
                public System.Type FileFolder;
                public string DirectoryName;
                public Boolean ISREADONLY;
                public long Length;
                public string Name;
            }
            static void Main(string[] args)
            {
                List<DirectoryFileInfo> filefolders = new List<DirectoryFileInfo>();
                DirectoryInfo dirinfo = new DirectoryInfo(@"c:\temp");
                FileInfo[] files = dirinfo.GetFiles();
                DirectoryInfo[] folders = dirinfo.GetDirectories();
                foreach(FileInfo file in  files)
                {
                    DirectoryFileInfo filefolder = new DirectoryFileInfo();
                    filefolder.FileFolder = file.GetType();
                    filefolder.DirectoryName = file.DirectoryName;
                    filefolder.ISREADONLY = file.IsReadOnly;
                    filefolder.Length = file.Length;
                    filefolder.Name = file.Name;
                    filefolders.Add(filefolder);
                }
                foreach (DirectoryInfo folder in folders)
                {
                    DirectoryFileInfo filefolder = new DirectoryFileInfo();
                    filefolder.FileFolder = folder.GetType();
                    filefolder.DirectoryName = folder.FullName;
                    filefolder.Name = folder.Name;
                    filefolders.Add(filefolder);
                }
            }
        }
    }


    jdweng

    Saturday, January 12, 2013 12:47 PM