Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 37 additions & 24 deletions src/GitHub.Exports/Services/VSServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio.TeamFoundation.Git.Extensibility;
using Microsoft.Win32;
using System.Diagnostics;

namespace GitHub.Services
{
Expand Down Expand Up @@ -147,36 +148,48 @@ static string PokeTheRegistryForLocalClonePath()
const string MRUKeyPath = "MRUSettingsLocalProjectLocationEntries";
public string SetDefaultProjectPath(string path)
{
string old;
using (var newProjectKey = Registry.CurrentUser.OpenSubKey(NewProjectDialogKeyPath, true))
var old = String.Empty;
try
{
using (var mruKey = newProjectKey?.OpenSubKey(MRUKeyPath, true))
var newProjectKey = Registry.CurrentUser.OpenSubKey(NewProjectDialogKeyPath, true) ??
Registry.CurrentUser.CreateSubKey(NewProjectDialogKeyPath);
Debug.Assert(newProjectKey != null, string.Format(CultureInfo.CurrentCulture, "Could not open or create registry key '{0}'", NewProjectDialogKeyPath));

using (newProjectKey)
{
if (mruKey == null)
return String.Empty;

// is this already the default path? bail
old = (string)mruKey.GetValue("Value0", string.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
if (String.Equals(path.TrimEnd('\\'), old.TrimEnd('\\'), StringComparison.CurrentCultureIgnoreCase))
return old;

// grab the existing list of recent paths, throwing away the last one
var numEntries = (int)mruKey.GetValue("MaximumEntries", 5);
var entries = new List<string>(numEntries);
for (int i = 0; i < numEntries - 1; i++)
var mruKey = newProjectKey.OpenSubKey(MRUKeyPath, true) ??
Registry.CurrentUser.CreateSubKey(MRUKeyPath);
Debug.Assert(mruKey != null, string.Format(CultureInfo.CurrentCulture, "Could not open or create registry key '{0}'", MRUKeyPath));

using (mruKey)
{
var val = (string)mruKey.GetValue("Value" + i, String.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
if (!String.IsNullOrEmpty(val))
entries.Add(val);
}
// is this already the default path? bail
old = (string)mruKey.GetValue("Value0", string.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mruKey could still be null here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't be null, it's the same logic as the newProjectKey, CreateSubKey will either return something or throw, in which case it will land on the exception handler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let's assert that then.

if (String.Equals(path.TrimEnd('\\'), old.TrimEnd('\\'), StringComparison.CurrentCultureIgnoreCase))
return old;

newProjectKey.SetValue("LastUsedNewProjectPath", path);
mruKey.SetValue("Value0", path);
// bump list of recent paths one entry down
for (int i = 0; i < entries.Count; i++)
mruKey.SetValue("Value" + (i+1), entries[i]);
// grab the existing list of recent paths, throwing away the last one
var numEntries = (int)mruKey.GetValue("MaximumEntries", 5);
var entries = new List<string>(numEntries);
for (int i = 0; i < numEntries - 1; i++)
{
var val = (string)mruKey.GetValue("Value" + i, String.Empty, RegistryValueOptions.DoNotExpandEnvironmentNames);
if (!String.IsNullOrEmpty(val))
entries.Add(val);
}

newProjectKey.SetValue("LastUsedNewProjectPath", path);
mruKey.SetValue("Value0", path);
// bump list of recent paths one entry down
for (int i = 0; i < entries.Count; i++)
mruKey.SetValue("Value" + (i + 1), entries[i]);
}
}
}
catch (Exception ex)
{
VsOutputLogger.WriteLine(string.Format(CultureInfo.CurrentCulture, "Error setting the create project path in the registry '{0}'", ex));
}
return old;
}

Expand Down