OLD | NEW |
(Empty) | |
| 1 /* |
| 2 Copyright 2013 Google Inc |
| 3 |
| 4 Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 you may not use this file except in compliance with the License. |
| 6 You may obtain a copy of the License at |
| 7 |
| 8 http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
| 10 Unless required by applicable law or agreed to in writing, software |
| 11 distributed under the License is distributed on an "AS IS" BASIS, |
| 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 See the License for the specific language governing permissions and |
| 14 limitations under the License. |
| 15 */ |
| 16 |
| 17 using System.Diagnostics; |
| 18 using System.IO; |
| 19 using System.Linq; |
| 20 |
| 21 namespace Google.Apis.Utils |
| 22 { |
| 23 /// <summary>Directory utilities which support copying, clearing and other o
perations on directories.</summary> |
| 24 public static class DirectoryUtilities |
| 25 { |
| 26 private static readonly TraceSource TraceSource = new TraceSource("Googl
e.Apis"); |
| 27 |
| 28 /// <summary>Copies a directory recursively to a new one.</summary> |
| 29 /// <param name="sourceDir">Source directory</param> |
| 30 /// <param name="destDir">Destination directory</param> |
| 31 internal static void CopyDirectory(string sourceDir, string destDir) |
| 32 { |
| 33 DirectoryInfo dir = new DirectoryInfo(sourceDir); |
| 34 if (!dir.Exists) |
| 35 { |
| 36 throw new DirectoryNotFoundException( |
| 37 string.Format("Source directory \"{0}\" does not exist", sou
rceDir)); |
| 38 } |
| 39 |
| 40 ClearOrCreateDirectory(destDir); |
| 41 |
| 42 // copy all files |
| 43 dir.GetFiles().ToList().ForEach( |
| 44 f => f.CopyTo(Path.Combine(destDir, f.Name))); |
| 45 |
| 46 // copy all nested directories |
| 47 dir.GetDirectories().ToList().ForEach( |
| 48 s => CopyDirectory(s.FullName, Path.Combine(destDir, s.Name))); |
| 49 } |
| 50 |
| 51 /// <summary>Clears or creates the specified directory.</summary> |
| 52 public static void ClearOrCreateDirectory(string dir) |
| 53 { |
| 54 if (Directory.Exists(dir)) |
| 55 { |
| 56 TraceSource.TraceEvent(TraceEventType.Information, "Clearing '{0
}'", dir); |
| 57 Directory.Delete(dir, true); |
| 58 } |
| 59 |
| 60 Directory.CreateDirectory(dir); |
| 61 TraceSource.TraceEvent(TraceEventType.Information, "Creating '{0}'",
dir); |
| 62 } |
| 63 |
| 64 /// <summary>Retrieves the relative path from the parent to the child.</
summary> |
| 65 public static string GetRelativePath(string child, string parent) |
| 66 { |
| 67 string absoluteChild = Path.GetFullPath(child); |
| 68 string absoluteParent = Path.GetFullPath(parent); |
| 69 |
| 70 if (!absoluteChild.StartsWith(absoluteParent + Path.DirectorySeparat
orChar)) |
| 71 { |
| 72 return absoluteChild; // Nothing in common. |
| 73 } |
| 74 |
| 75 return absoluteChild.Substring(absoluteParent.Length + 1); |
| 76 } |
| 77 } |
| 78 } |
OLD | NEW |