Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Deployer

, 30 Mar 2012 Apache
Automate deployment of Windows Services, ClickOnce, and other .NET applications.
deployer_src.zip
Deployer
Deployer
!copyToDllCache
Interop.VSFlex7L.dll
stdole.dll
ClassDiagram1.cd
clickOnceKey.pfx
deployer-full.xsx
Deployer.csproj.user
DeployTypes
DomainClasses
_tfs
text-base
ConfigReplacesEntry.cs.tfs-base
ConfigReplacesRoot.cs.tfs-base
DllCacheEntry.cs.tfs-base
_tfs
text-base
BaseDeployType.cs.tfs-base
ClickOnce.cs.tfs-base
DatabaseScript.cs.tfs-base
Service.cs.tfs-base
XCopy.cs.tfs-base
Logic
_tfs
text-base
Controller.cs.tfs-base
FileSystemUtil.cs.tfs-base
XmlUtil.cs.tfs-base
mage.exe
Properties
Settings.settings
_tfs
text-base
AssemblyInfo.cs.tfs-base
Resources.Designer.cs.tfs-base
Resources.resx.tfs-base
Settings.Designer.cs.tfs-base
Settings.settings.tfs-base
Zip
.svn
entries
format
prop-base
_tfs
text-base
props
_tfs
text-base
text-base
ZipArchive.cs.svn-base
ZipDirectory.cs.svn-base
ZipEntry.cs.svn-base
ZipEntryCollection.cs.svn-base
ZipException.cs.svn-base
ZipLib.cs.svn-base
ZipStream.cs.svn-base
_tfs
text-base
ZipArchive.cs.svn-base.tfs-base
ZipDirectory.cs.svn-base.tfs-base
ZipEntry.cs.svn-base.tfs-base
ZipEntryCollection.cs.svn-base.tfs-base
ZipException.cs.svn-base.tfs-base
ZipLib.cs.svn-base.tfs-base
ZipStream.cs.svn-base.tfs-base
tmp
prop-base
_tfs
text-base
props
_tfs
text-base
text-base
_tfs
text-base
_tfs
text-base
_tfs
text-base
entries.tfs-base
format.tfs-base
_tfs
text-base
ZipArchive.cs.tfs-base
ZipDirectory.cs.tfs-base
ZipEntry.cs.tfs-base
ZipEntryCollection.cs.tfs-base
ZipException.cs.tfs-base
ZipLib.cs.tfs-base
ZipStream.cs.tfs-base
zlib.dll
TestApp
Properties
Settings.settings
_tfs
text-base
AssemblyInfo.cs.tfs-base
Resources.Designer.cs.tfs-base
Resources.resx.tfs-base
Settings.Designer.cs.tfs-base
Settings.settings.tfs-base
TestClickOnceApp
ClickOnceApp_TemporaryKey.pfx
Properties
Settings.settings
_tfs
text-base
AssemblyInfo.cs.tfs-base
Resources.Designer.cs.tfs-base
Resources.resx.tfs-base
Settings.Designer.cs.tfs-base
Settings.settings.tfs-base
TestClickOnceApp.csproj.user
TestDatabaseScript
Properties
_tfs
text-base
AssemblyInfo.cs.tfs-base
TestWindowsService
Properties
_tfs
text-base
AssemblyInfo.cs.tfs-base
someFiles
_tfs
text-base
will not be included in backup.txt.tfs-base
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.IO;

namespace Deployer.Logic
{
    class FileSystemUtil
    {
        /// <summary>
        /// Wraps File class enabling coping of folders
        /// </summary>
        /// <param name="source">Source path</param>
        /// <param name="dest">Destination path</param>
        public static void CopyFolder(string source, string dest)
        {
            if (!dest.EndsWith("\\"))
                dest += "\\";

            if (!Directory.Exists(dest))
                Directory.CreateDirectory(dest);

            foreach (string file in Directory.GetFiles(source))
            {
                string fileDestionation = dest + Path.GetFileName(file);
                CopyFile(file, fileDestionation);
            }

            foreach (string folder in Directory.GetDirectories(source))
            {
                string subFolder = Path.GetFileName(folder);
                CopyFolder(folder, dest + "\\" + subFolder);
            }
        }

        /// <summary>
        /// Copy folder with option to exclude some folders/files
        /// </summary>
        /// <param name="source">Source path</param>
        /// <param name="dest">Destination path</param>
        /// <param name="excludePaths">List of strings of folders/files to exclude. If it is folder it must not end with \\ in order to have successful comparison</param>
        public static void CopyFolderWithExclude(string source, string dest, List<string> excludePaths)
        {
            if (excludePaths.Contains(source.TrimEnd('\\')))
                return;

            if (!dest.EndsWith("\\"))
                dest += "\\";

            if (!Directory.Exists(dest))
                Directory.CreateDirectory(dest);

            foreach (string file in Directory.GetFiles(source))
            {
                string fileDestionation = dest + Path.GetFileName(file);

                if (!excludePaths.Contains(file))
                    CopyFile(file, fileDestionation);
            }

            foreach (string folder in Directory.GetDirectories(source))
            {
                string subFolder = Path.GetFileName(folder);
                CopyFolderWithExclude(folder, dest + "\\" + subFolder, excludePaths);
            }
        }

        /// <summary>
        /// Wraps File.Copy method because of read only and system files
        /// </summary>
        /// <param name="source">Path to file</param>
        /// <param name="destionation">Path to file</param>
        public static void CopyFile(string source, string destionation)
        {
            if (File.Exists(destionation))
                File.Delete(destionation);

            File.Copy(source, destionation);
        }

        /// <summary>
        /// Delete file from disk even if it is temporary taken by some other program
        /// </summary>
        /// <param name="filePath">Path to file</param>
        public static void DeleteFile(string filePath)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(deleteFileProcess), filePath);
        }
        private static void deleteFileProcess(object o)
        {
            try
            {
                File.Delete(o.ToString());
            }
            catch
            {
                Thread.Sleep(5000);
                deleteFileProcess(o);
                return;
            }
        }

        /// <summary>
        /// Reading file from disk with silent failing if it is taken
        /// </summary>
        /// <param name="filePath">Path to file</param>
        /// <returns>FileInfo if file is successfuly read, null if it is not</returns>
        public static FileInfo TryToReadFile(string filePath)
        {
            try
            {
                File.ReadAllBytes(filePath);
                return new FileInfo(filePath);
            }
            catch
            {
                return null;
            }
        }


        /// <summary>
        /// Deletes all content from folder
        /// </summary>
        /// <param name="dir">Path to folder</param>
        public static void ClearFolder(DirectoryInfo dir)
        {
            foreach (DirectoryInfo di in dir.GetDirectories())
                di.Delete();

            foreach (FileInfo fi in dir.GetFiles())
                fi.Delete();
        }
    }
}

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 Apache License, Version 2.0

Share

About the Author

Predrag Tomasevic
Chief Technology Officer Atama Group
United States United States

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 30 Mar 2012
Article Copyright 2008 by Predrag Tomasevic
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid