locked
How to determine if a Visual Studio Project alrady contains a ProjectItem? RRS feed

  • Question

  • // open the solution

    solution.Open(solutionFileName);

     

    // here the solution is opened, now

    // each project needs to be updated

    foreach (EnvDTE.Project project in solution.Projects)

    {
       

     

        if (project.Name == visualStudioSolution.ApplicationLogicComponentProjectName)

        {

            // update the ApplicationLogicComponent Project

            tempAddedCount = UpdateProject(project, files, DataManager.ProjectTypeEnum.ALC);

        }

        // if this project name is the ApplicationLogicComponen project

        else if (project.Name == visualStudioSolution.DataAccessComponentProjectName)

        {

            // update the ApplicationLogicComponent Project

            tempAddedCount = UpdateProject(project, files, DataManager.ProjectTypeEnum.DAC);

        }

        // if this project name is the ObjectLibrary project

        else if (project.Name == visualStudioSolution.ObjectLibraryProjectName)

        {

            // update the ApplicationLogicComponent Project

            tempAddedCount = UpdateProject(project, files, DataManager.ProjectTypeEnum.ObjectLibrary);

        }

     

        // if one or more files were added

        if (tempAddedCount > 0)

        {

            // set new files added to true so

            // the solution gets saved

            newFilesAdded = true;

        }


    }

    Now in the UpdateProject method I have this code:

     

    foreach (ProjectFile projectFile in files)

    {

        // if this file should go in this project

        if (projectFile.ProjectType == projectType)

        {

            try

            {

                // add this file

                project.ProjectItems.AddFromFile(projectFile.FileName);

     

                // increment newFilesAddedCount

                newFilesAddedCount++;

            }

            catch (Exception error)

            {

                // for debugging only (for now)

                string err = error.ToString();

            }

        }

    }


    I have two problems that I am not sure how to handle:
    1. I need to make sure the file gets added to the correct folder (Not a Solution Folder, a directory folder that physcially exists on the hard drive). The file does exist in the correct folder on the hard drive, and the Visual Studio Project does have the folder already. Will project.ProjectItem add the file to the correct folder, or do I have to tell it where to add some way? If yes how?

    2. The Visual Studio Solution is not saving when I call solution.Close(true) - where true means save

    I am pretty sure the solution is failing because one or more files already exist in the project (I know it does in this test, I didnt add any new tables to the database so my code generator created files that I know already exists in the project).

    I tried iterating the ProjectItems in a project, and ProjectItem.Document does not exist for any of the files in my project.ProjectItems.

    Is there a way to do something like:

    if (project.ProjectItems.Contains(fileName))
    {
        // add the file
        project.ProjectItems.Add(fileName);
    }

    The only option I can think of I havent tried yet is to open the solution, add the file and close the solution, all in a try catch block but I would prefer to do it all at once as this would be much faster (there may be dozens or even hundreds of new files on databases with a large amount of tables.

    Thank you

     

    // iterate collection of ProjectFiles

     

        // if this project name is the ApplicationLogicComponen project

    Monday, January 25, 2010 2:33 PM

Answers

  • I figured it out, I was just about to post my solution.

    I am not using an Add-In, I am doing this from another app.

    The problem I was having was I wasnt checking the projectItem.ProjectItems (sub collection)

    so I added this method and I call it recursively until there are not any more sub projectItems.

    Thank you for replying so fast anyway.


    ///

    <summary>

    /// This method checks if a file exists in a project and call itself recursively until there are not any more sub ProjectItems.

    /// </summary>

    /// <param name="project"></param>

    /// <param name="projectFile"></param>

    /// <returns></returns>

    private static bool CheckIfFileExistsInProject(ProjectItems projectItems, ProjectFile projectFile)

    {

        // initial value

        bool fileExists = false;

     

        // iterate project items

        foreach(ProjectItem projectItem in projectItems)

        {

            // if the name matches

            if(projectItem.Name == projectFile.FileName)

            {

                // abort this add, file already exists

                fileExists = true;

     

                // break out of loop

                break;

            }

            else if ((projectItem.ProjectItems != null) && (projectItem.ProjectItems.Count > 0))

            {

                // check if the file exists in the project

                fileExists = CheckIfFileExistsInProject(projectItem.ProjectItems, projectFile);

     

                // if the file does exist

                if (fileExists)

                {

                    // abort this add, file already exists

                    fileExists = true;

                    // break out of loop

                    break;

                }

            }
        }

     

        // return value

        return fileExists;

    }

    #endregion

    • Marked as answer by Corby Nichols Monday, January 25, 2010 4:43 PM
    Monday, January 25, 2010 4:43 PM

All replies

  • Hello,

    1) You must iterate the files and folders of the project recursively:

    HOWTO: Navigate the files of a solution from a Visual Studio .NET macro or add-in.
    http://www.mztools.com/resources_vsnet_addins.aspx

    and then test the full file name of each ProjectItem (using ProjectItems.FileNames(1) for example)

    2) You can try Project.Save or Project.SaveAs to save the project (rather than the solution), or DTE.ExecuteCommand("File.SaveAll") to save everything.

    HTH

    MZ-Tools: Productivity add-ins for Visual Studio: http://www.mztools.com. My blog about developing add-ins: http://msmvps.com/blogs/carlosq/
    Monday, January 25, 2010 4:19 PM
  • I figured it out, I was just about to post my solution.

    I am not using an Add-In, I am doing this from another app.

    The problem I was having was I wasnt checking the projectItem.ProjectItems (sub collection)

    so I added this method and I call it recursively until there are not any more sub projectItems.

    Thank you for replying so fast anyway.


    ///

    <summary>

    /// This method checks if a file exists in a project and call itself recursively until there are not any more sub ProjectItems.

    /// </summary>

    /// <param name="project"></param>

    /// <param name="projectFile"></param>

    /// <returns></returns>

    private static bool CheckIfFileExistsInProject(ProjectItems projectItems, ProjectFile projectFile)

    {

        // initial value

        bool fileExists = false;

     

        // iterate project items

        foreach(ProjectItem projectItem in projectItems)

        {

            // if the name matches

            if(projectItem.Name == projectFile.FileName)

            {

                // abort this add, file already exists

                fileExists = true;

     

                // break out of loop

                break;

            }

            else if ((projectItem.ProjectItems != null) && (projectItem.ProjectItems.Count > 0))

            {

                // check if the file exists in the project

                fileExists = CheckIfFileExistsInProject(projectItem.ProjectItems, projectFile);

     

                // if the file does exist

                if (fileExists)

                {

                    // abort this add, file already exists

                    fileExists = true;

                    // break out of loop

                    break;

                }

            }
        }

     

        // return value

        return fileExists;

    }

    #endregion

    • Marked as answer by Corby Nichols Monday, January 25, 2010 4:43 PM
    Monday, January 25, 2010 4:43 PM