How to check if file is included in Project RRS feed

  • Question

  • I have been using this code below to include the files to the project.

    var project = new Project(projectFile);
    project.AddItem("Compile", fileToAdd);

    The file already exists in the project directory before the code is ran.

    The problem is if the file is already included in the solution, and this is ran again, there will be a duplicate of the same file.

    Is there are way to check if the file is already included in the solution?

    The code below only checks if the file exists physically in the project folder, but not if it is included in the solution.


    Friday, August 21, 2015 11:45 PM


  • Hi,

    We can use LINQ to find out the same file name in the Project Item which is itemType Compile.

    var project = new Project(projectFile);
    if (project.Items.Where(item => item.ItemType == "Compile" && item.EvaluatedInclude == fileToAdd).Count() == 0)
    project.AddItem("Compile", fileToAdd);

    Remark: EvaluatedInclude is case sensitive while file name compare.

    • Edited by stef chui Monday, August 24, 2015 9:20 AM remove the path.getfilenames function
    • Marked as answer by KKim777 Monday, August 24, 2015 3:32 PM
    Monday, August 24, 2015 8:52 AM