SystemPath.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 » SystemPath.cs

using System;
using System.IO;
using System.Text.RegularExpressions;

namespace ThoughtWorks.CruiseControl.Core.Util{
  public class SystemPath
  {
    public static readonly SystemPath Temp = new SystemPath(Path.GetTempPath());
    
    private readonly string path;

    public SystemPath(string path) : this(path, new ExecutionEnvironment())
    {
    }

    public SystemPath(string path, IExecutionEnvironment environment)
    {
      this.path = Convert(path, environment);
      if (PathIsInvalid(path)) throw new ArgumentException("Path contains invalid characters: " + path, "path");
    }

    public SystemPath Combine(string subpath)
    {
      return new SystemPath(Path.Combine(path, subpath));
    }

    private string Convert(string newpath, IExecutionEnvironment environment)
    {
      return Regex.Replace(newpath, @"[/\\]", environment.DirectorySeparator.ToString());  
    }
    
    public bool Exists()
    {
      return File.Exists(path);
    }

    public override string ToString()
    {
      return path;
    }
    
    public SystemPath CreateDirectory()
    {
      Directory.CreateDirectory(path);
      return this;
    }

    public void DeleteDirectory()
    {
      if (! Directory.Exists(path)) return;
      try { Directory.Delete(path, true); }
      catch (Exception e) { throw new IOException("Unable to delete directory: " + path, e); }
    }

        public void DeleteFile()
        {
            if (!File.Exists(path)) return;
            
            try { File.Delete(path); }
            catch (Exception e) { throw new IOException("Unable to delete file : " + path, e); }
            
        }


    public static SystemPath UniqueTempPath()
    {
      return Temp.Combine(Guid.NewGuid().ToString());
    }

    public static bool PathIsInvalid(string path)
    {
      return (-1 != path.IndexOfAny(Path.GetInvalidPathChars()));
    }

    public SystemPath CreateSubDirectory(string dir)
    {
      return Combine(dir).CreateDirectory();
    }

    public SystemPath CreateEmptyFile(string file)
    {
      return Combine(file).CreateEmptyFile();
    }
    
    public SystemPath CreateEmptyFile()
    {
      return CreateTextFile(string.Empty);
    }

    private SystemPath CreateTextFile(string content)
    {
      using (StreamWriter stream = File.CreateText(path))
      {
        stream.Write(content);
      }
      return this;
    }
    
    public string ReadTextFile()
    {
      using (StreamReader textReader = File.OpenText(path))
      {
        return textReader.ReadToEnd();
      }
    }

    public SystemPath CreateTextFile(string filename, string content)
    {
      return Combine(filename).CreateTextFile(content);
    }

  }
  
  public class TempDirectory : SystemPath, IDisposable
  {
    public TempDirectory() : base(UniqueTempPath().ToString())
    {
      CreateDirectory();
    }

    void IDisposable.Dispose()
    {
      DeleteDirectory();
    }    
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.