|
13 | 13 | using Microsoft.VisualStudio.Shell.Interop;
|
14 | 14 | using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
|
15 | 15 | using Microsoft.Win32;
|
| 16 | +using System.Diagnostics; |
16 | 17 |
|
17 | 18 | namespace GitHub.Services
|
18 | 19 | {
|
@@ -147,36 +148,48 @@ static string PokeTheRegistryForLocalClonePath()
|
147 | 148 | const string MRUKeyPath = "MRUSettingsLocalProjectLocationEntries";
|
148 | 149 | public string SetDefaultProjectPath(string path)
|
149 | 150 | {
|
150 |
| - string old; |
151 |
| - using (var newProjectKey = Registry.CurrentUser.OpenSubKey(NewProjectDialogKeyPath, true)) |
| 151 | + var old = String.Empty; |
| 152 | + try |
152 | 153 | {
|
153 |
| - using (var mruKey = newProjectKey?.OpenSubKey(MRUKeyPath, true)) |
| 154 | + var newProjectKey = Registry.CurrentUser.OpenSubKey(NewProjectDialogKeyPath, true) ?? |
| 155 | + Registry.CurrentUser.CreateSubKey(NewProjectDialogKeyPath); |
| 156 | + Debug.Assert(newProjectKey != null, string.Format(CultureInfo.CurrentCulture, "Could not open or create registry key '{0}'", NewProjectDialogKeyPath)); |
| 157 | + |
| 158 | + using (newProjectKey) |
154 | 159 | {
|
155 |
| - if (mruKey == null) |
156 |
| - return String.Empty; |
157 |
| - |
158 |
| - // is this already the default path? bail |
159 |
| - old = (string)mruKey.GetValue("Value0", string.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames); |
160 |
| - if (String.Equals(path.TrimEnd('\\'), old.TrimEnd('\\'), StringComparison.CurrentCultureIgnoreCase)) |
161 |
| - return old; |
162 |
| - |
163 |
| - // grab the existing list of recent paths, throwing away the last one |
164 |
| - var numEntries = (int)mruKey.GetValue("MaximumEntries", 5); |
165 |
| - var entries = new List<string>(numEntries); |
166 |
| - for (int i = 0; i < numEntries - 1; i++) |
| 160 | + var mruKey = newProjectKey.OpenSubKey(MRUKeyPath, true) ?? |
| 161 | + Registry.CurrentUser.CreateSubKey(MRUKeyPath); |
| 162 | + Debug.Assert(mruKey != null, string.Format(CultureInfo.CurrentCulture, "Could not open or create registry key '{0}'", MRUKeyPath)); |
| 163 | + |
| 164 | + using (mruKey) |
167 | 165 | {
|
168 |
| - var val = (string)mruKey.GetValue("Value" + i, String.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames); |
169 |
| - if (!String.IsNullOrEmpty(val)) |
170 |
| - entries.Add(val); |
171 |
| - } |
| 166 | + // is this already the default path? bail |
| 167 | + old = (string)mruKey.GetValue("Value0", string.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames); |
| 168 | + if (String.Equals(path.TrimEnd('\\'), old.TrimEnd('\\'), StringComparison.CurrentCultureIgnoreCase)) |
| 169 | + return old; |
172 | 170 |
|
173 |
| - newProjectKey.SetValue("LastUsedNewProjectPath", path); |
174 |
| - mruKey.SetValue("Value0", path); |
175 |
| - // bump list of recent paths one entry down |
176 |
| - for (int i = 0; i < entries.Count; i++) |
177 |
| - mruKey.SetValue("Value" + (i+1), entries[i]); |
| 171 | + // grab the existing list of recent paths, throwing away the last one |
| 172 | + var numEntries = (int)mruKey.GetValue("MaximumEntries", 5); |
| 173 | + var entries = new List<string>(numEntries); |
| 174 | + for (int i = 0; i < numEntries - 1; i++) |
| 175 | + { |
| 176 | + var val = (string)mruKey.GetValue("Value" + i, String.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames); |
| 177 | + if (!String.IsNullOrEmpty(val)) |
| 178 | + entries.Add(val); |
| 179 | + } |
| 180 | + |
| 181 | + newProjectKey.SetValue("LastUsedNewProjectPath", path); |
| 182 | + mruKey.SetValue("Value0", path); |
| 183 | + // bump list of recent paths one entry down |
| 184 | + for (int i = 0; i < entries.Count; i++) |
| 185 | + mruKey.SetValue("Value" + (i + 1), entries[i]); |
| 186 | + } |
178 | 187 | }
|
179 | 188 | }
|
| 189 | + catch (Exception ex) |
| 190 | + { |
| 191 | + VsOutputLogger.WriteLine(string.Format(CultureInfo.CurrentCulture, "Error setting the create project path in the registry '{0}'", ex)); |
| 192 | + } |
180 | 193 | return old;
|
181 | 194 | }
|
182 | 195 |
|
|
0 commit comments