开发者

Detect an open project in visual studio

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


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜