Skip to content

Commit 48a5153

Browse files
check all file associations
1 parent c1d9e84 commit 48a5153

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

src/Files.App/Helpers/Win32/Win32Helper.Storage.cs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,19 @@ public static partial class Win32Helper
2828
{
2929
public static async Task<string?> GetFileAssociationAsync(string filename, bool checkDesktopFirst = false)
3030
{
31-
// Find UWP apps
32-
async Task<string?> GetUwpAssoc()
33-
{
34-
var uwpApps = await Launcher.FindFileHandlersAsync(Path.GetExtension(filename));
35-
return uwpApps.Any() ? uwpApps[0].PackageFamilyName : null;
36-
}
37-
38-
// Find desktop apps
39-
string? GetDesktopAssoc()
40-
{
41-
var lpResult = new StringBuilder(2048);
42-
var hResult = Shell32.FindExecutable(filename, null, lpResult);
43-
44-
return hResult.ToInt64() > 32 ? lpResult.ToString() : null;
45-
}
46-
4731
if (checkDesktopFirst)
48-
return GetDesktopAssoc() ?? await GetUwpAssoc();
32+
return GetDesktopFileAssociation(filename) ?? (await GetUwpFileAssociations(filename)).FirstOrDefault();
4933

50-
return await GetUwpAssoc() ?? GetDesktopAssoc();
34+
return (await GetUwpFileAssociations(filename)).FirstOrDefault() ?? GetDesktopFileAssociation(filename);
35+
}
36+
37+
public static async Task<IEnumerable<string>> GetAllFileAssociationsAsync(string filename)
38+
{
39+
var uwpApps = await GetUwpFileAssociations(filename);
40+
var desktopApp = GetDesktopFileAssociation(filename);
41+
return desktopApp is not null
42+
? uwpApps.Append(desktopApp)
43+
: uwpApps;
5144
}
5245

5346
public static string ExtractStringFromDLL(string file, int number)
@@ -1073,5 +1066,17 @@ public static bool GetWin32FindDataForPath(string targetPath, out Win32PInvoke.W
10731066

10741067
return false;
10751068
}
1069+
private static async Task<IEnumerable<string>> GetUwpFileAssociations(string filename)
1070+
{
1071+
var uwpApps = await Launcher.FindFileHandlersAsync(Path.GetExtension(filename));
1072+
return uwpApps.Select(x => x.PackageFamilyName);
1073+
}
1074+
1075+
private static string? GetDesktopFileAssociation(string filename)
1076+
{
1077+
var lpResult = new StringBuilder(2048);
1078+
var hResult = Shell32.FindExecutable(filename, null, lpResult);
1079+
return hResult.ToInt64() > 32 ? lpResult.ToString() : null;
1080+
}
10761081
}
10771082
}

src/Files.App/Utils/Storage/StorageItems/ZipStorageFolder.cs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,11 @@ public static async Task<bool> CheckDefaultZipApp(string filePath)
9898
{
9999
Func<Task<bool>> queryFileAssoc = async () =>
100100
{
101-
var assoc = await Win32Helper.GetFileAssociationAsync(filePath);
102-
if (assoc is not null)
103-
{
104-
return assoc == Package.Current.Id.FamilyName
105-
|| assoc.EndsWith("Files.App\\Files.exe", StringComparison.OrdinalIgnoreCase)
106-
|| assoc.Equals(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "explorer.exe"), StringComparison.OrdinalIgnoreCase);
107-
}
108-
return true;
101+
var associations = await Win32Helper.GetAllFileAssociationsAsync(filePath);
102+
return associations.Any(assoc =>
103+
assoc == Package.Current.Id.FamilyName
104+
|| assoc.EndsWith("Files.App\\Files.exe", StringComparison.OrdinalIgnoreCase)
105+
|| assoc.Equals(IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "explorer.exe"), StringComparison.OrdinalIgnoreCase));
109106
};
110107
var ext = IO.Path.GetExtension(filePath)?.ToLowerInvariant();
111108
return await defaultAppDict.GetAsync(ext, queryFileAssoc);

0 commit comments

Comments
 (0)