Click here to Skip to main content
15,884,176 members
Articles / Programming Languages / C#

Project Tool

Rate me:
Please Sign up or sign in to vote.
4.69/5 (10 votes)
23 Sep 2007CPOL3 min read 54.5K   1.7K   73  
Backup your C# solution and projects for archive or source code sharing. Temporary or unused files are excluded.
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;

namespace QiHe.CodeLib
{
    /// <summary>
    /// FileHelper
    /// </summary>
    public class FileHelper
    {
        public static string StripExtension(string name)
        {
            int indexofdot = name.LastIndexOf('.');
            if (indexofdot == -1)
            {
                return name;
            }
            else
            {
                return name.Substring(0, indexofdot);
            }
        }

        public static string GetDirectory(string file)
        {
            FileInfo fi = new FileInfo(file);
            return fi.Directory.FullName;
        }

        public static void CreateDirectoryIfNotExist(string dir)
        {
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
        }

        public static string CompareFolder(string dir1, string dir2)
        {
            StringBuilder equal = new StringBuilder();
            StringBuilder notequal = new StringBuilder();
            DirectoryInfo di1 = new DirectoryInfo(dir1);
            DirectoryInfo di2 = new DirectoryInfo(dir2);
            foreach (FileInfo fi1 in di1.GetFiles())
            {
                foreach (FileInfo fi2 in di2.GetFiles())
                {
                    if (fi1.Name == fi2.Name)
                    {
                        if (CompareFile(fi1.FullName, fi2.FullName))
                        {
                            equal.AppendLine(fi1.Name);
                        }
                        else
                        {
                            notequal.AppendLine(fi1.Name);
                        }
                    }
                }
            }
            StringBuilder text = new StringBuilder();
            text.AppendLine("Equal Files:");
            text.AppendLine(equal.ToString());
            text.AppendLine("Not Equal Files:");
            text.AppendLine(notequal.ToString());
            return text.ToString();
        }

        public static bool CompareFile(string infile, string outfile)
        {
            using (FileStream input = new FileStream(infile, FileMode.Open, FileAccess.Read))
            {
                using (FileStream output = new FileStream(outfile, FileMode.Open, FileAccess.Read))
                {
                    if (input.Length != output.Length) return false;
                    while (true)
                    {
                        int inbyte = input.ReadByte();
                        int outbyte = output.ReadByte();
                        if (inbyte != outbyte) return false;
                        if (inbyte == -1) break;
                    }
                    return true;
                }
            }
        }

        /// <summary>
        /// Get relative file paths of all files in folder.
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public static List<string> GetFileList(string folder)
        {
            List<string> files = new List<string>();
            DirectoryInfo dir = new DirectoryInfo(folder);
            GetFileList(dir, null, files);
            return files;
        }

        private static void GetFileList(DirectoryInfo dir, string subpath, List<string> files)
        {
            foreach (FileInfo file in dir.GetFiles())
            {
                files.Add(subpath + file.Name);
            }
            foreach (DirectoryInfo subdir in dir.GetDirectories())
            {
                GetFileList(subdir, subpath + subdir.Name + "\\", files);
            }
        }

        public static List<string> GetFileList(string folder, char pathsep, bool includeDirectory)
        {
            List<string> files = new List<string>();
            DirectoryInfo dir = new DirectoryInfo(folder);
            GetFileList(dir, null, files, pathsep, includeDirectory);
            return files;
        }

        private static void GetFileList(DirectoryInfo dir, string subpath, List<string> files, char pathsep, bool includeDirectory)
        {
            if (includeDirectory && subpath != null)
            {
                files.Add(subpath);
            }
            foreach (FileInfo file in dir.GetFiles())
            {
                files.Add(subpath + file.Name);
            }
            foreach (DirectoryInfo subdir in dir.GetDirectories())
            {
                GetFileList(subdir, subpath + subdir.Name + pathsep, files, pathsep, includeDirectory);
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect YunCheDa Hangzhou
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions