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
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();
}
}
}
(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.
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):
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
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.