How do I find files that are missing from visual studio projects?

前端 未结 11 639
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-31 01:59

Due to issues with merging etc, we have lots of project files that don’t contain all source code files that are within their folders.

Before I write a little tool, that

相关标签:
11条回答
  • 2021-01-31 02:26

    A small C# console application that finds all cs files referred from project files under a certain root folder (recursively), and compares with files in the file system under the same root folder. Can be applied to different file extensions, and different project file structures (I have tested it for VS2008). It may need some modifications to suit other needs, but should provide a useful base.

    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text.RegularExpressions;
    
    namespace CompareProjectFilesWithFileSystem
    {
        class Program
        {
            static void Main(string[] args)
            {
                string ext = "cs";
                string rootProjectFolder = @"C:\MySolutionRootFolder";
                Regex projectFileReferenceRegEx = new Regex(@"<(Compile|Content|None) Include=\""([^\""]+." + ext + @")\""( /)?>");
    
                // Files in file system:
                List<string> filesInFileSystem = new List<string>();
                filesInFileSystem.AddRange(Directory.GetFiles(rootProjectFolder, "*." + ext, SearchOption.AllDirectories));
    
                // Files referred from project files:
                string[] projectFilePaths = Directory.GetFiles(rootProjectFolder, "*.csproj", SearchOption.AllDirectories);
                List<string> filesReferredInProjectFiles = new List<string>();
                foreach (string projectFilePath in projectFilePaths)
                {
                    string[] lines = File.ReadAllLines(projectFilePath);
                    foreach (string line in lines)
                    {
                        Match match = projectFileReferenceRegEx.Match(line);
                        if (match.Success)
                        {
                            filesReferredInProjectFiles.Add(Path.Combine(Path.GetDirectoryName(projectFilePath), match.Result("$2")));
                        }
                    }
                }
    
                // Finding files referred from project files that are not contained in file system. And reversely.
                var list1 = filesReferredInProjectFiles.Except(filesInFileSystem).ToList();
                var list2 = filesInFileSystem.Except(filesReferredInProjectFiles).ToList();
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-31 02:27

    (Assuming that Team Foundation Server is your source control): If the file has been added to the project and shows up in Source Control Explorer, but does NOT show up in Solution Explorer, you must manually add it to Source Control Explorer by selecting "Add", "Existing Item". This is an annoying Visual Studio/TFS bug that I just spend 2 hours trying to figure out.

    0 讨论(0)
  • 2021-01-31 02:33

    With that number of files and projects, it sounds like you might want something more automated. However, there is a manual approach (not sure if you are already aware of it):

    1. Select the project in Solution Explorer pane
    2. Project menu -> Show all Files
    3. Under the project, files which are not part of the project will show up as "ghost" icons
    4. Select the file(s) you want and pick "Include In Project" from the context menu
    0 讨论(0)
  • 2021-01-31 02:35

    We had a similar situation which resulted in failed compiles due to missing files. I stumbled upon this post linked below, which helped me. It describes writing a Visual Studio macro that runs when a build is started.

    Report error/warning if missing files in project/solution in Visual Studio

    0 讨论(0)
  • 2021-01-31 02:37

    If anyone has the error File Not Included the first response dated 2011 still works on VS 2017 Version 15.8.5 on .NET Framework Version 4.7.03056 Windows 10 with latest updates. Closing and reopening VS shows all files.

    0 讨论(0)
提交回复
热议问题