views:

44

answers:

1

I'm creating a visual studio add in that adds files to the current project that's open. How can I detect what's currently open? As I need to retrieve the folder/file location.

A: 

You can grab all the projects in a solution using the following code:

private DTE2 dte2;

public void OnConnection(object application, ext_ConnectMode connectMode, object addInInst, ref Array custom)
{
    dte2 = (DTE2)application;
    var events = (Events2)dte2.Events;
    solutionEvents = events.SolutionEvents;

    solutionEvents.Opened += OnSolutionOpened;
}

private void OnSolutionOpened()
{
    Projects projects = dte2.Solution.Projects;

    foreach (Project project in projects)
    {
        ProcessProject(project);
    }
}

private void ProcessProject(Project project)
{
    string directoryName = Path.GetDirectoryName(project.FileName);
    string fileName = Path.GetFileName(project.FileName);

    if (directoryName == null || fileName == null)
    {
        return;
    }

    var directory = new DirectoryInfo(directoryName);
    var fileInfo = new FileInfo(fileName);

    //do work
}
Chris Martin