using Microsoft.VisualStudio.Shell;
using System;
using System.Threading;
using Task = System.Threading.Tasks.Task;
using System.Collections.Generic;
using System.Collections;
namespace VSIXProject1
{
[PackageRegistration(UseManagedResourcesOnly = true, AllowsBackgroundLoading = true)]
[System.Runtime.InteropServices.Guid(VSIXProject1Package.PackageGuidString)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.NoSolution, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.EmptySolution, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionExists, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionHasSingleProject, PackageAutoLoadFlags.BackgroundLoad)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids.SolutionHasMultipleProjects, PackageAutoLoadFlags.BackgroundLoad)]
public sealed class VSIXProject1Package : AsyncPackage
{
public const string PackageGuidString = "<GUID>";
protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress<ServiceProgressData> progress)
{
await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);
_dte = (EnvDTE80.DTE2)await this.GetServiceAsync(typeof(EnvDTE.DTE));
if (_dte == null)
{
return;
}
_cev = _dte.Events.CommandEvents;
_cev.BeforeExecute += _cev_BeforeExecute;
}
private EnvDTE80.DTE2 _dte;
private EnvDTE.CommandEvents _cev;
// Project.AddExistingItem
private Guid targetGuid = new Guid("{5EFC7975-14BC-11CF-9B2B-00AA00573819}");
private int targetID = 244;
private void _cev_BeforeExecute(string Guid, int ID, object CustomIn, object CustomOut, ref bool CancelDefault)
{
ThreadHelper.ThrowIfNotOnUIThread();
if (ID == targetID && new Guid(Guid) == targetGuid)
{
CancelDefault = true;
var cmd = _dte.Commands.Item(Guid, ID).Name;
var dlg = new System.Windows.Forms.OpenFileDialog();
if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
return;
}
//Check Here
if (System.IO.Path.GetExtension(dlg.FileName).ToLower() != ".txt")
{
return;
}
if (_dte.ActiveSolutionProjects is IList list
&& list.Count > 0
&& list[0] is EnvDTE.Project proj)
{
proj.ProjectItems.AddFromFile(dlg.FileName);
}
}
}
}
}
How to add file type validation , when trying to add existing item in solution explorer of visual studio experimental instance?
Mekala Meghana
20
Reputation points
I am trying to add file type validation in "add existing item" in solution explorer in visual studio experimental instance. If the selected file type is not valid, then it needs to display message box like invalid file type. Else it needs to add the file to the project. Currently by using this "VS.Events.ProjectItemsEvents.AfterAddProjectItems", it is adding the file before validating file type. Is there any visual studio event to call at the time of "add existing Item" to validate?
Accepted answer
-
gekka 9,341 Reputation points MVP
2024-02-06T09:51:24.2233333+00:00