static bool FolderRecursivly(string sourceFolderPath, string destinationFolderPath, ref bool undo, string renameFolderTo = "") { sourceFolderPath = EMPath.AddSlash(sourceFolderPath); destinationFolderPath = EMPath.AddSlash(destinationFolderPath); try { DirectoryInfo sourceFolder = new DirectoryInfo(sourceFolderPath); if (renameFolderTo == string.Empty) { destinationFolderPath = EMPath.AddSlash(destinationFolderPath + sourceFolder.Name); } else { destinationFolderPath = EMPath.AddSlash(destinationFolderPath + renameFolderTo); } //check if trying to copy in same parent folder if (destinationFolderPath.ToLower() == sourceFolderPath.ToLower()) { UserInfoHandler.ShowError("Cannot duplicate folder '" + sourceFolder.Name + "' in its parent folder."); return(false); } //check if source folder already exists at destination path if (Directory.Exists(destinationFolderPath)) { if (UserInfoHandler.GetInfo("Folder '" + destinationFolderPath + "' already exists. Should it be deleted?", MessageBoxButtons.OKCancel) == DialogResult.Cancel) { return(false); } Directory.Delete(destinationFolderPath, true); } //create source folder at destination path Directory.CreateDirectory(destinationFolderPath); undo = true; //copy all files directly under this folder FileInfo[] subFiles = sourceFolder.GetFiles("*.*"); if (subFiles != null) { foreach (FileInfo subFile in subFiles) { File.Copy(subFile.FullName, destinationFolderPath + subFile.Name); } } //find all subfolders under this folder for recursive call foreach (DirectoryInfo subFolder in sourceFolder.GetDirectories()) { FolderRecursivly(subFolder.FullName, destinationFolderPath, ref undo); } } catch (Exception exception) { UserInfoHandler.ShowException(exception, "Close and reopen the user interface and try again."); return(false); } return(true); }
// this reads the ProjectSettings.xml if it exists and returns the ProjectName field if it exists, or null otherwise private static string GetProjectNameOrDefault() { string brandName = null; string projectSettingsFullPath = new EMPath(EM_AppContext.FolderEuromodFiles).GetProjectSettingsPath(true); if (File.Exists(projectSettingsFullPath)) { try { using (XmlReader xmlReader = XmlReader.Create(projectSettingsFullPath, new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Fragment })) { xmlReader.Read(); while (xmlReader.NodeType != XmlNodeType.None && (xmlReader.NodeType != XmlNodeType.Element || xmlReader.Name != "ProjectSettings")) { xmlReader.Read(); } if (xmlReader.NodeType == XmlNodeType.None) { UserInfoHandler.ShowError($"File '{projectSettingsFullPath}' does not contain a valid XML structure." + Environment.NewLine + "Alternative brand is ignored."); } XElement settingsElement = XElement.ReadFrom(xmlReader) as XElement; foreach (XElement xe in settingsElement.Elements()) { if (xe.Value == null) { continue; } switch (GetXEleName(xe)) { case BRAND_ELEMENT_PROJECT_NAME: brandName = xe.Value; break; default: break; // unknown tags are simply ignored for now } } ; } } catch { } } return(String.IsNullOrEmpty(brandName) ? "EUROMOD" : brandName); // return the brand or }