private void MenuItemCallback(object sender, EventArgs e) { UIHierarchyItem item = GetSelectedItem(); if (item == null) { return; } string folder = FindFolder(item); if (string.IsNullOrEmpty(folder) || !Directory.Exists(folder)) { return; } // See if the user has a valid selection on the Solution Tree and avoid prompting the user // for a file name. Project project = GetActiveProject(); if (project == null) { return; } string defaultExt = GetProjectDefaultExtension(project); string input = PromptForFileName( folder, defaultExt ).TrimStart('/', '\\').Replace("/", "\\"); if (string.IsNullOrEmpty(input)) { return; } if (input.EndsWith("\\", StringComparison.Ordinal)) { input = input + "__dummy__"; } TemplateMap templates = GetTemplateMap(); string projectPath = Path.GetDirectoryName(project.FullName); string relativePath; if (folder.StartsWith(projectPath, StringComparison.OrdinalIgnoreCase) && folder.Length > projectPath.Length) { relativePath = CombinePaths(folder.Substring(projectPath.Length + 1), input); // I'm intentionally avoiding the use of Path.Combine because input may contain pattern characters // such as ':' which will cause Path.Combine to handle differently. We simply need a string concat here. } else { relativePath = input; } try { var itemManager = new ProjectItemManager(_dte, templates); var creator = itemManager.GetCreator(project, projectPath, relativePath); var info = creator.Create(project); SelectCurrentItem(); if (info != ItemInfo.Empty && _lastUsedExtension != defaultExt && info.Extension == _lastUsedExtension) { // TODO: Save extension to project-specific storage _overridingExtension = info.Extension; } _lastUsedExtension = info.Extension; } catch (Exception ex) { MessageBox.Show(ex.Message, "Cannot Add New File", MessageBoxButton.OK, MessageBoxImage.Error); } }