ExeSessionProcess.GetExecutablePath() doesn't search in 'Program Files (x86)' directory

Advertisement

tattdogg
Joined:
Posts:
19
Location:
Minsk, Belarus

ExeSessionProcess.GetExecutablePath() doesn't search in 'Program Files (x86)' directory

Hi

ExeSessionProcess.GetExecutablePath() doesn't search in Program Files (x86) directory

Possible fix: https://github.com/mirror/winscp/pull/1/commits/0996f04cda7ca0d90febce16e0f3da986709d820
diff --git a/dotnet/internal/ExeSessionProcess.cs b/dotnet/internal/ExeSessionProcess.cs
index 99a1d15e..abd4f082 100644
--- a/dotnet/internal/ExeSessionProcess.cs
+++ b/dotnet/internal/ExeSessionProcess.cs
@@ -749,7 +749,8 @@ private string GetExecutablePath()
                     if (!TryFindExecutableInPath(GetAssemblyPath(), out executablePath) &&
                         !TryFindExecutableInPath(GetInstallationPath(Registry.CurrentUser), out executablePath) &&
                         !TryFindExecutableInPath(GetInstallationPath(Registry.LocalMachine), out executablePath) &&
-                        !TryFindExecutableInPath(GetDefaultInstallationPath(), out executablePath))
+                        !TryFindExecutableInPath(GetDefaultInstallationPathOnx64(), out executablePath) &&
+                        !TryFindExecutableInPath(GetDefaultInstallationPathOnx86(), out executablePath))
                     {
                         throw new SessionLocalException(_session,
                             string.Format(CultureInfo.CurrentCulture,
@@ -761,10 +762,15 @@ private string GetExecutablePath()
             }
         }
 
-        private static string GetDefaultInstallationPath()
+        private static string GetDefaultInstallationPathOnx64()
         {
             return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "WinSCP");
         }
+        
+        private static string GetDefaultInstallationPathOnx86()
+        {
+            return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "WinSCP");
+        }        
 
         private static string GetInstallationPath(RegistryKey rootKey)
         {

Also you can add search in %PATH% variable:
Environment.GetEnvironmentVariable("PATH")?.Split(';').SingleOrDefault(x => x.EndsWith("\\WinSCP\\"));

Thanks, tattdogg

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
42,732
Location:
Prague, Czechia

Re: ExeSessionProcess.GetExecutablePath() doesn't search in 'Program Files (x86)' directory

Thanks for spotting this.

The ProgramFilesX86 requires .NET 4, so I cannot use it.
Also what primarily needs fixing, is the GetInstallationPath implementation.

Anyway, both are fixed:
Issue 1421 – WinSCP .NET assembly cannot find WinSCP executable in its installation folder nor the default installation folder when used in 64-bit process

Though note that you generally should not rely on WinSCP.exe in "Program Files". You should distribute the executable with your .NET application.
https://winscp.net/eng/docs/library_install

The lookup in "Program Files" is really only the last resort fallback.

Reply with quote

Advertisement

You can post new topics in this forum