Extension methods (C#)


public static class StringExtension
    {
        /// <summary>
        /// Gets string as array of lines.
        /// </summary>
        /// <param name="value">The value. Text to process</param>
        /// <returns></returns>
        public static string[] GetAsLines(this string value)
        {
            string[] myLines = value.Split(Environment.NewLine.ToCharArray());
            return myLines;
        }
    }
    public static class DirectoryExtension
    {
        /// <summary>
        /// List of all sub-directories recursively 
        /// </summary>
        /// <param name="directoryInfo">The directory info.</param>
        /// <returns></returns>
        public static List<DirectoryInfo> AllDescendants(this DirectoryInfo directoryInfo)
        {
            if (!Directory.Exists(directoryInfo.FullName)) 
            { 
                throw (new DirectoryNotFoundException()); 
            }

            List<DirectoryInfo> myAllDescendants = GetSubDirectories(directoryInfo);
            return myAllDescendants;

        }
        /// <summary>
        /// List of all files in all sub-directories
        /// </summary>
        /// <param name="directoryInfo">The directory info.</param>
        /// <returns></returns>
        public static List<FileInfo> AllFileDescendants(this DirectoryInfo directoryInfo)
        {
            IEnumerable<FileInfo> myFiles =
                from myDirInfo in directoryInfo.AllDescendants()
                    .SelectMany(dir => dir.GetFiles())
                select myDirInfo;

            return myFiles.ToList();
        }

        /// <summary>
        /// Gets the sub-directories.
        /// Main part of the recursion
        /// </summary>
        /// <param name="directoryInfo">The directory info.</param>
        /// <returns></returns>
        private static List<DirectoryInfo> GetSubDirectories(DirectoryInfo directoryInfo)
        {
            var myDirectories = new List<DirectoryInfo>();
            DirectoryInfo[] subDirectories = directoryInfo.GetDirectories();

            myDirectories.AddRange(subDirectories);

            foreach (DirectoryInfo dirInfo in subDirectories)
            {
                myDirectories.AddRange(GetSubDirectories(dirInfo));
            }

            return myDirectories;
        }
    }
   public static class FileInfoExtension
    {
        /// <summary>
        /// Get text content of the file 
        /// </summary>
        /// <param name="fileInfo">The file info.</param>
        /// <returns></returns>
        public static string GetTextContent(this FileInfo fileInfo)
        {
            String myFileContent;

            using (StreamReader myReader = new StreamReader(fileInfo.FullName))
            {
                myFileContent = myReader.ReadToEnd();

                myReader.DiscardBufferedData();
                myReader.Close();
                myReader.Dispose();

            }
            return myFileContent;
        }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s