在visual studio中检测一个打开的项目

时间:2010-09-24 21:27:44

标签: c# visual-studio

我正在创建一个visual studio add in,它将文件添加到当前打开的项目中。如何检测当前打开的内容?因为我需要检索文件夹/文件位置。

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码获取解决方案中的所有项目:

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
}