From a239e92bc0bc27cf91b0f3fd30f259f0a011e2db Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 20 Sep 2017 19:03:00 +0200 Subject: [PATCH] Fix LocalDb detection on 32/64 bits systems --- src/Umbraco.Core/Persistence/LocalDb.cs | 31 ++++++++++++++++++------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/Umbraco.Core/Persistence/LocalDb.cs b/src/Umbraco.Core/Persistence/LocalDb.cs index f706979e17..4dcc56a9a4 100644 --- a/src/Umbraco.Core/Persistence/LocalDb.cs +++ b/src/Umbraco.Core/Persistence/LocalDb.cs @@ -24,6 +24,7 @@ namespace Umbraco.Core.Persistence { private int _version; private bool _hasVersion; + private string _exe; #region Availability & Version @@ -84,16 +85,31 @@ namespace Umbraco.Core.Persistence { _hasVersion = true; _version = -1; + _exe = null; var programFiles = Environment.GetEnvironmentVariable("ProgramFiles"); - if (programFiles == null) return; + + // MS SQL Server installs in e.g. "C:\Program Files\Microsoft SQL Server", so + // we want to detect it in "%ProgramFiles%\Microsoft SQL Server" - however, if + // Umbraco runs as a 32bits process (e.g. IISExpress configured as 32bits) + // on a 64bits system, %ProgramFiles% will point to "C:\Program Files (x86)" + // and SQL Server cannot be found. But then, %ProgramW6432% will point to + // the original "C:\Program Files". Using it to fix the path. + // see also: MSDN doc for WOW64 implementation + // + var programW6432 = Environment.GetEnvironmentVariable("ProgramW6432"); + if (string.IsNullOrWhiteSpace(programW6432) == false && programW6432 != programFiles) + programFiles = programW6432; + + if (string.IsNullOrWhiteSpace(programFiles)) return; // detect 14, 13, 12, 11 for (var i = 14; i > 10; i--) { - var path = Path.Combine(programFiles, string.Format(@"Microsoft SQL Server\{0}0\Tools\Binn\SqlLocalDB.exe", i)); - if (File.Exists(path) == false) continue; + var exe = Path.Combine(programFiles, string.Format(@"Microsoft SQL Server\{0}0\Tools\Binn\SqlLocalDB.exe", i)); + if (File.Exists(exe) == false) continue; _version = i; + _exe = exe; break; } } @@ -897,16 +913,13 @@ namespace Umbraco.Core.Persistence /// private int ExecuteSqlLocalDb(string args, out string output, out string error) { - var programFiles = Environment.GetEnvironmentVariable("ProgramFiles"); - if (programFiles == null) + if (_exe == null) // should never happen - we should not execute if not available { output = string.Empty; error = "SqlLocalDB.exe not found"; return -1; } - var path = Path.Combine(programFiles, string.Format(@"Microsoft SQL Server\{0}0\Tools\Binn\SqlLocalDB.exe", _version)); - var p = new Process { StartInfo = @@ -914,7 +927,7 @@ namespace Umbraco.Core.Persistence UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, - FileName = path, + FileName = _exe, Arguments = args, CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden @@ -929,7 +942,7 @@ namespace Umbraco.Core.Persistence } /// - /// Returns a Unicode string with the delimiters added to make the input string a valid SQL Server delimited identifier. + /// Returns a Unicode string with the delimiters added to make the input string a valid SQL Server delimited identifier. /// /// The name to quote. /// A quote character.