public static void CopyDirectory(string sourceDirectoryPath, string destinationDirectoryPath, CopyDirectoryInfo directoryInfo) { sourceDirectoryPath = Path.Combine(sourceDirectoryPath, directoryInfo.Source); destinationDirectoryPath = Path.Combine(destinationDirectoryPath, directoryInfo.Destination ?? directoryInfo.Source); Debug.Log(string.Format(CultureInfo.InvariantCulture, @"{0} ""{1}\{2}"" to ""{3}""", directoryInfo.Recursive ? "Recursively copying" : "Copying", sourceDirectoryPath, directoryInfo.Filter, destinationDirectoryPath)); foreach (string sourceFilePath in Directory.GetFiles(sourceDirectoryPath, directoryInfo.Filter, directoryInfo.Recursive ? SearchOption.AllDirectories : SearchOption.TopDirectoryOnly)) { string destinationFilePath = sourceFilePath.Replace(sourceDirectoryPath, destinationDirectoryPath); try { Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath)); if (File.Exists(destinationFilePath)) { File.SetAttributes(destinationFilePath, FileAttributes.Normal); } File.Copy(sourceFilePath, destinationFilePath, true); File.SetAttributes(destinationFilePath, FileAttributes.Normal); } catch (Exception exception) { Debug.LogError(string.Format(CultureInfo.InvariantCulture, "Failed to copy \"{0}\" to \"{1}\" with \"{2}\"", sourceFilePath, destinationFilePath, exception)); } } }
private static IEnumerable<CopyDirectoryInfo> ReadCopyList(XmlTextReader reader) { List<CopyDirectoryInfo> result = new List<CopyDirectoryInfo>(); while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: if (string.Equals(reader.Name, "Copy", StringComparison.InvariantCultureIgnoreCase)) { string source = null; string dest = null; string filter = null; bool recursive = false; while (reader.MoveToNextAttribute()) { if (string.Equals(reader.Name, "Source", StringComparison.InvariantCultureIgnoreCase)) { source = reader.Value; } else if (string.Equals(reader.Name, "Destination", StringComparison.InvariantCultureIgnoreCase)) { dest = reader.Value; } else if (string.Equals(reader.Name, "Recursive", StringComparison.InvariantCultureIgnoreCase)) { recursive = System.Convert.ToBoolean(reader.Value); } else if (string.Equals(reader.Name, "Filter", StringComparison.InvariantCultureIgnoreCase)) { filter = reader.Value; } } if (source != null) { // Either the file specifies the Destination as well, or else CopyDirectory will use Source for Destination CopyDirectoryInfo info = new CopyDirectoryInfo(); info.Source = source; if (dest != null) { info.Destination = dest; } if (filter != null) { info.Filter = filter; } info.Recursive = recursive; Debug.Log(string.Format(CultureInfo.InvariantCulture, @"Build: Adding {0}copy ""{1}\{2}"" => ""{3}""", info.Recursive ? "Recursive " : "", info.Source, info.Filter, info.Destination ?? info.Source)); result.Add(info); } } break; case XmlNodeType.EndElement: if (string.Equals(reader.Name, "CopyList", StringComparison.InvariantCultureIgnoreCase)) return result; break; } } return result; }