IoService.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » Core » Util » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » Core » Util » IoService.cs
using System;
using System.IO;

namespace ThoughtWorks.CruiseControl.Core.Util{
    public class IoService : IFileDirectoryDeleter
    {
        private readonly IExecutionEnvironment executionEnvironment = new ExecutionEnvironment();

        public void DeleteIncludingReadOnlyObjects(string path)
        {
            try
            {
                // check whether path is a file or directory
                if (File.Exists(path))
                {
                    File.SetAttributes(path, FileAttributes.Normal);
                    File.Delete(path);
                }
                else if (Directory.Exists(path))
                {
                    var dirInfo = new DirectoryInfo(path);
                    SetReadOnlyRecursive(dirInfo);
                    dirInfo.Delete(true);
                }
                else
                {
                    Log.Warning("[IoService] File or directory not found: '{0}'", path);
                }
            }
            catch (PathTooLongException pathTooLongEx)
            {
                Log.Error("[IoService] Unable to delete path '{0}'.{1}{2}", path, Environment.NewLine, pathTooLongEx);
                
                if (executionEnvironment.IsRunningOnWindows)
                {
                    DeleteDirectoryWithLongPath(path);
                }
                else
                {
                    throw;
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
        }

        /// <summary>
        /// Deletes a directory on Windows with a commandline call.
        /// 
        /// Reason:
        /// .NET only supports filenames up to 260 characters long for backwards compability
        /// read more at: http://blogs.msdn.com/bclteam/archive/2007/02/13/long-paths-in-net-part-1-of-3-kim-hamilton.aspx
        /// this is a Windows only limitation
        /// </summary>
        /// <param name="path">Path to delete.</param>
        static void DeleteDirectoryWithLongPath(string path)
        {
            Log.Info("[IoService] Try running 'cmd.exe /C RD /S /Q' to delete '{0}'", path);

            // call a commandline delete as fallback
            var executor = new ProcessExecutor();
            var processInfo = new ProcessInfo("cmd.exe",
                string.Concat("/C RD /S /Q ", StringUtil.AutoDoubleQuoteString(path)));

            var pr = executor.Execute(processInfo);
            if (pr.Failed)
                throw new CruiseControlException(string.Format("Unable to delete path '{0}'.", path));
        }

        /// <summary>
        /// Sets directory and file attributes to "normal" recursive
        /// </summary>
        /// <param name="path">Root path to start from.</param>
        static void SetReadOnlyRecursive(DirectoryInfo path)
        {

            foreach (var dirInfo in path.GetDirectories())
            {
                try
                {
                    dirInfo.Attributes = FileAttributes.Normal;
                }
                catch (Exception ex)
                {
                    Log.Error("[IoService] Unable to remove read-only attribute from '{0}'.", dirInfo.FullName);
                    Log.Error(ex);
                    throw;
                }

                SetReadOnlyRecursive(dirInfo);
            }

            foreach (var fileInfo in path.GetFiles())
            {
                try
                {
                    fileInfo.Attributes = FileAttributes.Normal;
                }
                catch (Exception ex)
                {
                    Log.Error("[IoService] Unable to remove read-only attribute from '{0}'.", fileInfo.FullName);
                    Log.Error(ex);
                    throw;
                }
            }
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.