private void btnConnect_Click(object sender, EventArgs e) { if (txtServerUrl.Enabled) { string strServerUrl = txtServerUrl.Text; try { // get the configuartion service #if UsingVssClientCredentials // TODO: find a way for TFS 2017 to always ask for credentials srvConfiguration = new TfsConfigurationServer(new Uri(strServerUrl), new Microsoft.VisualStudio.Services.Client.VssClientCredentials(new Microsoft.VisualStudio.Services.Common.WindowsCredential(true), Microsoft.VisualStudio.Services.Common.CredentialPromptType.PromptIfNeeded)); #else srvConfiguration = new TfsConfigurationServer(new Uri(strServerUrl), null, new UICredentialsProvider(this)); #endif // get the registry service tfsRegServiceConfiguration = srvConfiguration.GetService <ITeamFoundationRegistry>(); ReadRegistry(); } catch (Exception ex) { MessageBox.Show(ex.Message); return; } EnableControls(true); } else { EnableControls(false); } }
/// <summary> /// Load settings from registry or applies default values if registry values are either missing or /// this class was created without a connection to a team foundation server /// </summary> /// <param name="teamProjectCollectionUrl">Team project collection url</param> /// <returns>True if all keys were accessed successfully, false if at least on key could not be accessed</returns> public bool Load(string teamProjectCollectionUrl) { var successfulLoad = true; Logger.Instance().Log(TraceLevel.Verbose, "Loading settings from team foundation registry"); if (teamProjectCollectionUrl != null) { var tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(teamProjectCollectionUrl)); tfs.EnsureAuthenticated(); _registry = tfs.GetService <ITeamFoundationRegistry>(); } // Loads values from TFS registry foreach (var property in GetType().GetProperties(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { var registryInfo = property.GetCustomAttribute <TfsRegistryEntryAttribute>(); // ReSharper disable InvertIf if (registryInfo != null) // ReSharper restore InvertIf { var key = string.Format("{0}/{1}", TfsRegistryFolder, registryInfo.RegistryKeyOverride ?? property.Name); try { // Apply default value and after that, try to load value from registry property.SetValue(this, registryInfo.DefaultValue); var value = _registry.GetValue(key); if (value == null) { // If we dont have read permission for security reasons null is returned which is the same for keys that don't exist Logger.Instance().Log(TraceLevel.Verbose, "Key not present in registry or access denied: {0}. Using default value", key); successfulLoad = false; } else { property.SetValue(this, Convert.ChangeType(value, property.PropertyType)); } } catch (Exception e) { Logger.Instance().Log(TraceLevel.Warning, "Failed to load value {0} from tfs registry: {1}", key, e.ToString()); } } } return(successfulLoad); }