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
2024-02-06T06:55:22.69+00:00

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?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,821 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,573 questions
Visual Studio Extensions
Visual Studio Extensions
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Extensions: A program or program module that adds functionality to or extends the effectiveness of a program.
189 questions
0 comments No comments
{count} votes

Accepted answer
  1. gekka 7,986 Reputation points MVP
    2024-02-06T09:51:24.2233333+00:00
    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);
                    }
    
                }
            }
        }
    }
    

0 additional answers

Sort by: Most helpful