我创建了一个简单的visual studio 2015扩展,为当前项目添加了一个类。
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重新加载它?
答案 0 :(得分: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);