Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » C# » Utilities » Downloads
 
Add your own
alternative version

Project Tool

, 23 Sep 2007 CPOL
Backup your C# solution and projects for archive or source code sharing. Temporary or unused files are excluded.
projecttool.zip
ProjectTool.exe
projecttool_0908_.zip
bin
Release
ICSharpCode.SharpZipLib.dll
ProjectTool.exe
CodeLib
Controls
ProjectTool
Items
Project
Propertys
Solution
Icon
cs.png
default.ico
dir.png
diro.png
dll.png
projecttool_20070924.zip
ProjectTool.exe
Checksums
Compress
Deflate
Common
Decode
Encode
Formats
Zip
HuffmanCoding
LempelZivWelch
LempelZiv
Resources
Icon
cs.png
default.ico
dir.png
diro.png
dll.png
projecttool_src.zip
ProjectTool.exe
cs.png
default.ico
dir.png
diro.png
dll.png
release.zip
ICSharpCode.SharpZipLib.dll
ProjectTool.exe
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)

Share

About the Author

Liu Junfeng
Software Developer (Senior) Beyondsoft SH
China China
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411023.1 | Last Updated 23 Sep 2007
Article Copyright 2006 by Liu Junfeng
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid