2017-05-22 2 views
0

저는 현재 프로젝트에 클래스를 추가하는 간단한 Visual Studio 2015 확장을 만들었습니다.Visual Studio Extension Reload Project

Project p = (Project) ProjectsComboBox.SelectedItem; 

string projectLocation = Path.GetDirectoryName(p.FileName); 
// I load the project where I want to Add file 
var projectCurrent = new Microsoft.Build.Evaluation.Project(p.FileName); 


string relativeNewPath = "\\Model\\DAL\\" + ViewNameTexBox.Text + "DAO.cs"; 
string newPath = projectLocation + relativeNewPath; 

projectCurrent.AddItem("Compile", relativeNewPath); 
projectCurrent.Save(); 

// Once class added to my project I edit text inside newly create class 
string text = File.ReadAllText("Templates/DAO.cs.txt"); 
text = text.Replace("$viewName$", ViewNameTexBox.Text); 
File.WriteAllText(newPath, text); 

파일이 프로젝트에 포함되어 있으며 수정되었지만 visual studio에서 프로젝트를 다시로드하라는 메시지를 표시합니다. 그렇게하면 프로젝트가 다시로드되지 않습니다.

프로젝트

환경

내가 currentProject을 처분하고 싶지만 프로젝트는 IDisposable 아닌 외부에서 수정되었습니다. currentProject를 null로 설정해도 아무 효과가 없습니다.

어떻게하면 currentProject를 비우고 Visual Studio에서 다시로드하도록 할 수 있습니까?

+1

이유 [태그 : C] 태그? –

+0

@IanAbbott 클릭 자동 완성을 놓쳤습니다. C로 변경했습니다. – Yvain

+0

다른 방법으로이 기능을 찾았습니다.이 답변을 참조하십시오. [post] (https://stackoverflow.com/a/44160484/1317323) – Yvain

답변

0

프로젝트에 파일을 추가하기 전에 프로젝트를 언로드 한 다음 프로젝트를 다시로드하십시오. 다음 코드를 참조하십시오.

Microsoft.Build.Evaluation.ProjectCollection projectCollection = new Microsoft.Build.Evaluation.ProjectCollection(); 

var projectCurrent = projectCollection.LoadProject(projectPath); 
projectCollection.UnloadProject(projectCurrent); 

string relativeNewPath = "\\Model\\DAL\\DAO.cs"; 
string newPath = projectPath + relativeNewPath; 
projectCurrent.AddItem("Compile", relativeNewPath); 
projectCurrent.Save(); 
projectCollection.LoadProject(projectPath); 

enter image description here

+0

이 솔루션을 사용하면 프로젝트 링크 파일은 프로젝트 루트에서 추가하지만 실제 파일은 올바른 위치 (\\ Model \\ DAO)에 있습니다. 또한 프로젝트를 다시로드 할 수 있지만 이번에는 다시로드 할 수 있습니다. – Yvain

+0

발견! relativePath는 "\\ Model \\ DAL"대신 "Model \\ DAL"이어야합니다. – Yvain

관련 문제