Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 700cc32

Browse files
committed
Merge pull request #148 from github/shana/147-create-project-path
Fix setting create project dialog path when keys don't exist
2 parents 84db796 + 64fdc91 commit 700cc32

File tree

1 file changed

+37
-24
lines changed

1 file changed

+37
-24
lines changed

src/GitHub.Exports/Services/VSServices.cs

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Microsoft.VisualStudio.Shell.Interop;
1414
using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
1515
using Microsoft.Win32;
16+
using System.Diagnostics;
1617

1718
namespace GitHub.Services
1819
{
@@ -147,36 +148,48 @@ static string PokeTheRegistryForLocalClonePath()
147148
const string MRUKeyPath = "MRUSettingsLocalProjectLocationEntries";
148149
public string SetDefaultProjectPath(string path)
149150
{
150-
string old;
151-
using (var newProjectKey = Registry.CurrentUser.OpenSubKey(NewProjectDialogKeyPath, true))
151+
var old = String.Empty;
152+
try
152153
{
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)
154159
{
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)
167165
{
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;
172170

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+
}
178187
}
179188
}
189+
catch (Exception ex)
190+
{
191+
VsOutputLogger.WriteLine(string.Format(CultureInfo.CurrentCulture, "Error setting the create project path in the registry '{0}'", ex));
192+
}
180193
return old;
181194
}
182195

0 commit comments

Comments
 (0)