好吧,我有一個解決辦法:
string GetWebConfigXml() {
string configXml = null;
Window window = null;
ProjectItem project = null;
ProjectItem configFile = null;
try {
EnvDTE.DTE dte = Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(DTE)) as DTE;
if(dte == null) return null;
project = dte.Solution.FindProjectItem(dte.ActiveDocument.FullName);
configFile = (from ProjectItem childItem in project.ProjectItems
where childItem.Name.Equals("web.config", StringComparison.OrdinalIgnoreCase)
select childItem).FirstOrDefault();
if (configFile == null) return null;
if (!configFile.IsOpen) window = configFile.Open();
var selection = (TextSelection)configFile.Document.Selection;
selection.SelectAll();
configXml = selection.Text;
} finally {
//Clean up the COM stuff
if (window != null) {
window.Close(vsSaveChanges.vsSaveChangesNo);
window = null;
}
if (configFile != null) {
configFile = null;
}
if (project != null) {
project = null;
}
}
}
return configXml;
}
注:不要忘記你可能需要嘗試捕撈的船載在這裏