public static void SaveConfigs(SettingsArray settings) { Status.Update("Writing settings ... ", false); var project = DteHelper.GetSelectedProject(); var file = project.GetProjectDirectory() + "\\" + FILE_NAME; var newLine = false; if (!File.Exists(file)) { File.Create(file).Dispose(); project.ProjectItems.AddFromFile(file); Status.Update("Created a new settings file."); newLine = true; } // check out file if in TFS try { var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(file); if (workspaceInfo != null) { var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri); var workspace = workspaceInfo.GetWorkspace(server); var pending = workspace.GetPendingChanges(new[] { file }); if (!pending.Any()) { workspace.Get(new[] { file }, VersionSpec.Latest, RecursionType.Full, GetOptions.GetAll | GetOptions.Overwrite); Status.Update((newLine ? "" : "\n") + "\tRetrieved latest settings file from TFS' current workspace."); workspace.PendEdit(file); Status.Update("\tChecked out settings file from TFS' current workspace."); newLine = true; } } } catch (Exception) { // ignored } using (var stream = File.Open(file, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) { // clear the file to start from scratch stream.SetLength(0); var bformatter = new BinaryFormatter { Binder = new Binder() }; bformatter.Serialize(stream, settings); Status.Update(newLine ? "Done writing settings!" : "done!"); } }
/// <summary> /// Retrieves the connection strings section from the selected project in VS. /// </summary> public static ConnectionStringsSection GetSection(EnvDTE.DTE vs) { if (vs == null) { return(null); } return(GetSection(DteHelper.GetSelectedProject(vs))); }
/// <summary> /// Sets the value. /// </summary> /// <param name="currentValue">The current value.</param> /// <param name="newValue">The new value.</param> /// <returns></returns> private bool SetValue(object currentValue, out object newValue) { if (currentValue == null) { DTE dte = (DTE)GetService(typeof(DTE)); newValue = DteHelperEx.GetCodeDomProvider(DteHelper.GetSelectedProject(dte)); return(true); } newValue = null; return(false); }
/// <summary> /// Provides the list of valid values to choose from for the connection string. /// </summary> public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context) { ConnectionStringsSection section = null; if (String.IsNullOrEmpty(configurationArgument)) { EnvDTE.DTE vs = (EnvDTE.DTE)context.GetService(typeof(EnvDTE.DTE)); if (vs == null) { return(new StandardValuesCollection(null)); } EnvDTE.Project project = DteHelper.GetSelectedProject(vs); if (project == null) { return(new StandardValuesCollection(null)); } section = ConnectionSettingsInfo.GetSection(vs); } else { IDictionaryService dictionary = (IDictionaryService)context.GetService(typeof(IDictionaryService)); EnvDTE.ProjectItem configurationItem = dictionary.GetValue(configurationArgument) as EnvDTE.ProjectItem; if (configurationItem != null) { section = ConnectionSettingsInfo.GetSection(configurationItem.get_FileNames(1)); } } if (section == null) { return(new StandardValuesCollection(null)); } List <ConnectionStringSettings> connectionslist = new List <ConnectionStringSettings>(section.ConnectionStrings.Count); foreach (ConnectionStringSettings setting in section.ConnectionStrings) { connectionslist.Add(setting); } connectionslist.Sort(CompareConnectionStringSettings); return(new StandardValuesCollection(connectionslist)); }
public static SettingsArray LoadConfigs() { try { Status.Update("Reading settings ... ", false); var project = DteHelper.GetSelectedProject(); var file = project.GetProjectDirectory() + "\\" + FILE_NAME; var newLine = false; if (File.Exists(file)) { // get latest file if in TFS try { var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(file); if (workspaceInfo != null) { var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri); var workspace = workspaceInfo.GetWorkspace(server); var pending = workspace.GetPendingChanges(new[] { file }); if (!pending.Any()) { workspace.Get(new[] { file }, VersionSpec.Latest, RecursionType.Full, GetOptions.GetAll | GetOptions.Overwrite); Status.Update("\n\tRetrieved latest settings file from TFS' current workspace."); newLine = true; } } } catch (Exception) { // ignored } //Open the file written above and read values from it. using (var stream = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read)) { var bformatter = new BinaryFormatter { Binder = new Binder() }; var settingsObject = bformatter.Deserialize(stream); SettingsArray settings; if (settingsObject is SettingsArray) { settings = (SettingsArray)settingsObject; } else { throw new Exception("Invalid settings format."); } Status.Update(newLine ? "Done reading settings!" : "done!"); return(settings); } } else { Status.Update("[ERROR] settings file does not exist."); } } catch (Exception ex) { Status.Update("failed to read settings => " + ex.Message); } var newSettings = new SettingsArray(); newSettings.SettingsList.Add(new Settings()); newSettings.SelectedSettingsIndex = 0; return(newSettings); }