private async System.Threading.Tasks.Task AddFileAsync(string name, NewItemTarget target, bool isEmbeddedResource = false) { await JoinableTaskFactory.SwitchToMainThreadAsync(); FileInfo file; // If the file is being added to a solution folder, but that // solution folder doesn't have a corresponding directory on // disk, then write the file to the root of the solution instead. if (target.IsSolutionFolder && !Directory.Exists(target.Directory)) { file = new FileInfo(Path.Combine(Path.GetDirectoryName(_dte.Solution.FullName), Path.GetFileName(name))); } else { file = new FileInfo(Path.Combine(target.Directory, name)); } // Make sure the directory exists before we create the file. Don't use // `PackageUtilities.EnsureOutputPath()` because it can silently fail. Directory.CreateDirectory(file.DirectoryName); if (!file.Exists) { Project project; if (target.IsSolutionOrSolutionFolder) { project = GetOrAddSolutionFolder(Path.GetDirectoryName(name), target); } else { project = target.Project; } int position = await WriteFileAsync(project, file.FullName); if (target.ProjectItem != null && target.ProjectItem.IsKind(Constants.vsProjectItemKindVirtualFolder)) { target.ProjectItem.ProjectItems.AddFromFile(file.FullName); } else { project.AddFileToProject(file, isEmbeddedResource: isEmbeddedResource); } VsShellUtilities.OpenDocument(this, file.FullName); // Move cursor into position. if (position > 0) { Microsoft.VisualStudio.Text.Editor.IWpfTextView view = ProjectHelpers.GetCurentTextView(); if (view != null) { view.Caret.MoveTo(new SnapshotPoint(view.TextBuffer.CurrentSnapshot, position)); } } ExecuteCommandIfAvailable("SolutionExplorer.SyncWithActiveDocument"); _dte.ActiveDocument.Activate(); } else { MessageBox.Show($"The file '{file}' already exists.", Vsix.Name, MessageBoxButton.OK, MessageBoxImage.Information); } }
//private void MenuItem_BeforeQueryStatus(object sender, EventArgs e) //{ // var button = (OleMenuCommand)sender; // button.Visible = button.Enabled = false; // UIHierarchyItem item = GetSelectedItem(); // var project = item.Object as Project; // if (project == null || !project.Kind.Equals(EnvDTE.Constants.vsProjectKindSolutionItems, StringComparison.OrdinalIgnoreCase)) // button.Visible = button.Enabled = true; //} private async void ExecuteAsync(object sender, EventArgs e) { object item = ProjectHelpers.GetSelectedItem(); string folder = FindFolder(item); if (string.IsNullOrEmpty(folder) || !Directory.Exists(folder)) { return; } var selectedItem = item as ProjectItem; var selectedProject = item as Project; Project project = selectedItem?.ContainingProject ?? selectedProject ?? ProjectHelpers.GetActiveProject(); if (project == null) { return; } dynamic inputValues = PromptForFileName(folder); string input = inputValues.Input.TrimStart('/', '\\').Replace("/", "\\"); if (string.IsNullOrEmpty(input)) { return; } string[] parsedInputs = GetParsedInput(input); foreach (string inputItem in parsedInputs) { input = inputItem; if (input.EndsWith("\\", StringComparison.Ordinal)) { input = input + "__dummy__"; } else { // only prepend the date/time onto filenames (not folders) input = input.Insert(input.LastIndexOf("\\") + 1, DateTime.Now.ToString("yyyyMMdd_HHmmss_")); } var file = new FileInfo(Path.Combine(folder, input)); string dir = file.DirectoryName; PackageUtilities.EnsureOutputPath(dir); if (!file.Exists) { int position = await WriteFileAsync(project, file.FullName); try { ProjectItem projectItem = null; if (item is ProjectItem projItem) { if ("{6BB5F8F0-4483-11D3-8BCF-00C04F8EC28C}" == projItem.Kind) // Constants.vsProjectItemKindVirtualFolder { projectItem = projItem.ProjectItems.AddFromFile(file.FullName); } } if (projectItem == null) { project.AddFileToProject(file.FullName, isEmbeddedResource: (bool)inputValues.IsEmbeddedResource); } if (file.FullName.EndsWith("__dummy__")) { projectItem?.Delete(); continue; } VsShellUtilities.OpenDocument(this, file.FullName); // Move cursor into position if (position > 0) { Microsoft.VisualStudio.Text.Editor.IWpfTextView view = ProjectHelpers.GetCurentTextView(); if (view != null) { view.Caret.MoveTo(new SnapshotPoint(view.TextBuffer.CurrentSnapshot, position)); } } // I have no idea why but only doing Activate once after SyncwithActiveDocument does not work 100% of the time. _dte.ActiveDocument.Activate(); _dte.ExecuteCommand("SolutionExplorer.SyncWithActiveDocument"); _dte.ActiveDocument.Activate(); } catch (Exception ex) { Logger.Log(ex); } } else { System.Windows.Forms.MessageBox.Show("The file '" + file + "' already exist."); } } }