using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.MSBuild; string solutionPath = @"C:\solution.sln"; string projectName = "MyProject"; string filePath = @"C:\MyProject\File.cs"; using var workspace = MSBuildWorkspace.Create(); // Open the solution var solution = await workspace.OpenSolutionAsync(solutionPath); // Find the project by name var project = solution.Projects.Single(p => p.Name == projectName); // Find the document ID for the specific file var documentId = project.GetDocumentIdsWithFilePath(filePath).Single();
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.MSBuild; string solutionPath = @"C:\solution.sln"; string projectName = "MyProject"; string documentPath = @"C:\MyProject\File.cs"; using var workspace = MSBuildWorkspace.Create(); // Open the solution var solution = await workspace.OpenSolutionAsync(solutionPath); // Find the project by name var project = solution.Projects.Single(p => p.Name == projectName); // Find the document ID for the specific file var documentId = project.GetDocumentIdsWithFilePath(documentPath).Single(); // Get the document from the workspace using the ID var document = solution.GetDocument(documentId);In both examples, we are using the Microsoft.CodeAnalysis package to work with DocumentIds. Specifically, we are using the `Microsoft.CodeAnalysis.MSBuild` namespace to load and manipulate projects within a solution.