Skip to content

Commit cf9d325

Browse files
jpobstjonpryor
authored andcommitted
[AndroidSdkWindows] Guard against exception checking registry (#79)
Fixes: https://developercommunity.visualstudio.com/content/problem/883179/ilegal-characters-in-path-after-fresh-install.html Somehow, a registry key contains invalid path characters. This causes an `ArgumentException` from `AndroidSdkWindows.CheckRegistryKeyForExecutable()`: System.ArgumentException: Illegal characters in path. at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional) at System.IO.Path.Combine(String path1, String path2) at Xamarin.Android.Tools.AndroidSdkWindows.CheckRegistryKeyForExecutable(UIntPtr key, String subkey, String valueName, Wow64 wow64, String subdir, String exe) at Xamarin.Android.Tools.AndroidSdkWindows.<GetAllAvailableAndroidSdks>d__37.MoveNext() at System.Linq.Enumerable.<DistinctIterator>d__64`1.MoveNext() at System.Linq.Buffer`1..ctor(IEnumerable`1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source) at Xamarin.Android.Tools.AndroidSdkBase.get_AllAndroidSdks() at Xamarin.Android.Tools.AndroidSdkBase.Initialize(String androidSdkPath, String androidNdkPath, String javaSdkPath) at Xamarin.Android.Tools.AndroidSdkWindows.Initialize(String androidSdkPath, String androidNdkPath, String javaSdkPath) at Xamarin.Android.Tools.AndroidSdkInfo..ctor(Action`2 logger, String androidSdkPath, String androidNdkPath, String javaSdkPath) Handle the `ArgumentException` by wrapping the body of `CheckRegistryKeyForExecutable()` within a `try/catch` block and catch all exceptions. If an exception is thrown, there is no executable of interest.
1 parent 310c5cf commit cf9d325

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/Xamarin.Android.Tools.AndroidSdk/Sdks/AndroidSdkWindows.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,19 +303,23 @@ public override void SetPreferredAndroidNdkPath (string path)
303303
#region Helper Methods
304304
private static bool CheckRegistryKeyForExecutable (UIntPtr key, string subkey, string valueName, RegistryEx.Wow64 wow64, string subdir, string exe)
305305
{
306-
string key_name = string.Format (@"{0}\{1}\{2}", key == RegistryEx.CurrentUser ? "HKCU" : "HKLM", subkey, valueName);
306+
try {
307+
string key_name = string.Format (@"{0}\{1}\{2}", key == RegistryEx.CurrentUser ? "HKCU" : "HKLM", subkey, valueName);
307308

308-
var path = NullIfEmpty (RegistryEx.GetValueString (key, subkey, valueName, wow64));
309+
var path = NullIfEmpty (RegistryEx.GetValueString (key, subkey, valueName, wow64));
309310

310-
if (path == null) {
311-
return false;
312-
}
311+
if (path == null) {
312+
return false;
313+
}
313314

314-
if (!ProcessUtils.FindExecutablesInDirectory (Path.Combine (path, subdir), exe).Any ()) {
315+
if (!ProcessUtils.FindExecutablesInDirectory (Path.Combine (path, subdir), exe).Any ()) {
316+
return false;
317+
}
318+
319+
return true;
320+
} catch (Exception) {
315321
return false;
316322
}
317-
318-
return true;
319323
}
320324
#endregion
321325

0 commit comments

Comments
 (0)