Working with projects in Visual Studio extensions

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Here's a collection of small code samples on different ways to work with projects.

Get project from contained file

This is how to get the project from one if its files.

 string fileName = "c:\\file\\in\\project.txt";
 PhysicalFile item = await PhysicalFile.FromFileAsync(fileName);
 Project project = item.ContainingProject;

Add files to project

Here's how to add files from disk to the project.

Project project = await VS.Solutions.GetActiveProjectAsync();

var file1 = "c:\\file\\in\\project\\1.txt";
var file2 = "c:\\file\\in\\project\\2.txt";
var file3 = "c:\\file\\in\\project\\3.txt";

await project.AddExistingFilesAsync(file1, file2, file3);

Find type of project

Find out what type of project you're dealing with.

bool isCsharp = await project.IsKindAsync(ProjectTypes.CSHARP);