Visual Studio Extension Reload Project

半城伤御伤魂 提交于 2019-12-24 21:24:06

问题


I created a simple visual studio 2015 extension that adds a class to the current 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);

The file is included to the project and modified but visual studio prompt a message asking to reload project. When I do so, the project won't reload.

the project has been modified outside the environment

I would like to dispose currentProject but project is not IDisposable. Setting currentProject to null has no effect.

How can I tell my code to free currentProject and let Visual Studio reload it ?


回答1:


You can unload the project before add files to project, then load project again, The following code for your reference.

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);



来源:https://stackoverflow.com/questions/44117270/visual-studio-extension-reload-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!