/// <summary> /// Returns a prefered valid solution item name from given name, by removing invalid characters and spaces. /// </summary> public static string MakePreferredSolutionItemName(string value) { return(DataFormats.MakeValidSolutionItemName(value).Replace(@" ", string.Empty)); }
public static string Rename(this IItemContainer item, string proposedItemName, bool ensureUniqueness = false, IVsUIShell uiService = null) { Guard.NotNull(() => item, item); Guard.NotNullOrEmpty(() => proposedItemName, proposedItemName); // Ensure new name is cleaned (i.e. valid solution name) var newItemName = proposedItemName; if (!DataFormats.IsValidSolutionItemName(proposedItemName)) { newItemName = DataFormats.MakeValidSolutionItemName(proposedItemName); } // Ensure rename required if (newItemName.Equals(item.Name, StringComparison.OrdinalIgnoreCase)) { return(newItemName); } // If Item, then ensure the new name has an extension (borrowed from existing). if (item is IItem) { var proposedExtension = Path.GetExtension(proposedItemName); if (string.IsNullOrEmpty(proposedExtension)) { var existingExtension = Path.GetExtension(item.Name); if (!string.IsNullOrEmpty(existingExtension)) { newItemName = Path.ChangeExtension(newItemName, existingExtension); } } } // Check to see if new named item already exists (within parent) bool newNamedItemExists = (item.Parent.Items.Where(i => i.Name.Equals(newItemName, StringComparison.OrdinalIgnoreCase)).FirstOrDefault() != null); if (newNamedItemExists) { if (ensureUniqueness) { // Calculate new unique name var uniqueItemName = string.Empty; uniqueItemName = (item is IItem) ? item.Parent.CalculateNextUniqueChildItemName <IItem>(newItemName) : uniqueItemName; uniqueItemName = (item is IProject) ? item.Parent.CalculateNextUniqueChildItemName <IProject>(newItemName) : uniqueItemName; if (string.IsNullOrEmpty(uniqueItemName)) { throw new InvalidOperationException( string.Format(CultureInfo.CurrentCulture, Resources.SolutionExtensions_ErrorRenameUnknownType, item.Kind)); } // Prompt user interactively if (newNamedItemExists) { if (uiService != null) { uiService.ShowWarning(Resources.SolutionExtensions_PromptItemAlreadyExists_Title, string.Format(CultureInfo.CurrentCulture, Resources.SolutionExtensions_PromptItemAlreadyExists_Message, newItemName, uniqueItemName)); } } newItemName = uniqueItemName; } else { throw new InvalidOperationException( string.Format(CultureInfo.CurrentCulture, Resources.SolutionExtensions_ErrorRenamedItemExists, newItemName)); } } // Rename item if (item is IItem || item is IFolder) { var vsItem = item.As <ProjectItem>(); if (vsItem != null) { vsItem.Name = newItemName; } } // Rename project var project = item as IProject; if (project != null) { var vsProject = project.As <Project>(); if (vsProject != null) { // Rename project Name vsProject.Name = newItemName; // Rename AssemblyName if (project.Data != null) { if (project.Data.AssemblyName != null) { var assemblyName = project.Data.AssemblyName as string; if (assemblyName.IndexOf(project.Name, StringComparison.OrdinalIgnoreCase) >= 0) { project.Data.AssemblyName = assemblyName.Replace(project.Name, newItemName); } } // Rename Rootnamespace if (project.Data.DefaultNamespace != null) { var defaultNamespace = project.Data.DefaultNamespace as string; if (defaultNamespace.IndexOf(project.Name, StringComparison.OrdinalIgnoreCase) >= 0) { project.Data.DefaultNamespace = defaultNamespace.Replace(project.Name, newItemName); } } } } } return(newItemName); }