How do I programmatically refresh/reload a VS project after modifying the underlying file?

痴心易碎 提交于 2019-12-19 03:23:15

问题


I am developing a Visual Studio package and I have written some code that will make a file in Solution Explorer dependant upon another file.

What this means is that it gives them the same relationship as code-behind files or designer files, where they appear nested under the parent file with a plus/minus icon.

+ MainForm.cs


- MainForm.cs
   MainForm.Designer.cs
   MainForm.resx

The code that I have written successfully and correctly modifies the underlying project file, however the change is not reflected in Solution Explorer until the project is closed and re-opened.

I'm looking for some code that will refresh or reload the project so that the change is visible in Solution Explorer immediately.

Further Information...

Here is the sudo code that demonstrates the mechanism by which I create the dependant file.

IVsBuildPropertyStorage vsBuildPropertyStorage = GetBuildPropertyStorage();
vsBuildPropertyStorage.SetItemAttribute(projectItemIdentifier, "DependentUpon", parentFileName);

I have also tried adding this in an attempt to get the project to reload, but it doesn't have any effect.

project.Save();
VSProject obj = project.Object as VSProject;
obj.Refresh();

回答1:


AFAIK the only way of doing this is via automation of the Solution Explorer tool-window:

EnvDTE.DTE dte = ...;

string solutionName = Path.GetFileNameWithoutExtension(dte.Solution.FullName);
string projectName = project.Name;

dte.Windows.Item(EnvDTE.Constants.vsWindowKindSolutionExplorer).Activate();
((DTE2)dte).ToolWindows.SolutionExplorer.GetItem(solutionName + @"\" + projectName).Select(vsUISelectionType.vsUISelectionTypeSelect);

dte.ExecuteCommand("Project.UnloadProject");
dte.ExecuteCommand("Project.ReloadProject");

Note that, if the project hasn't been saved, the user will get a dialog box prior to the "Project.UnloadProject" call.



来源:https://stackoverflow.com/questions/12320527/how-do-i-programmatically-refresh-reload-a-vs-project-after-modifying-the-underl

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