views:

119

answers:

1

I have a WF service with a custom activity and a custom designer (WPF). I want to add a validation that will check for the presence of some value in the web.config file.

At runtime I can overload void CacheMetadata(ActivityMetadata metadata) and thus I can do the validation happily there using System.Configuration.ConfigurationManager to read the config file.

Since I also want to do this at design time, I was looking for a way to do this in the designer.

A: 

Ok I have one solution:

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

Note: Don't forget you'll probably need a boat load of try catches in here

Preet Sangha