Skip to content

Commit f032a36

Browse files
marcofranzen99yaira2
authored andcommitted
check all file associations
1 parent 183f073 commit f032a36

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
@@ -165,26 +165,19 @@ public static Task StartSTATask(Action action)
165165

166166
public static async Task<string?> GetFileAssociationAsync(string filename, bool checkDesktopFirst = false)
167167
{
168-
// Find UWP apps
169-
async Task<string?> GetUwpAssoc()
170-
{
171-
var uwpApps = await Launcher.FindFileHandlersAsync(Path.GetExtension(filename));
172-
return uwpApps.Any() ? uwpApps[0].PackageFamilyName : null;
173-
}
174-
175-
// Find desktop apps
176-
string? GetDesktopAssoc()
177-
{
178-
var lpResult = new StringBuilder(2048);
179-
var hResult = Shell32.FindExecutable(filename, null, lpResult);
180-
181-
return hResult.ToInt64() > 32 ? lpResult.ToString() : null;
182-
}
183-
184168
if (checkDesktopFirst)
185-
return GetDesktopAssoc() ?? await GetUwpAssoc();
169+
return GetDesktopFileAssociation(filename) ?? (await GetUwpFileAssociations(filename)).FirstOrDefault();
186170

187-
return await GetUwpAssoc() ?? GetDesktopAssoc();
171+
return (await GetUwpFileAssociations(filename)).FirstOrDefault() ?? GetDesktopFileAssociation(filename);
172+
}
173+
174+
public static async Task<IEnumerable<string>> GetAllFileAssociationsAsync(string filename)
175+
{
176+
var uwpApps = await GetUwpFileAssociations(filename);
177+
var desktopApp = GetDesktopFileAssociation(filename);
178+
return desktopApp is not null
179+
? uwpApps.Append(desktopApp)
180+
: uwpApps;
188181
}
189182

190183
public static string ExtractStringFromDLL(string file, int number)
@@ -1210,5 +1203,17 @@ public static bool GetWin32FindDataForPath(string targetPath, out Win32PInvoke.W
12101203

12111204
return false;
12121205
}
1206+
private static async Task<IEnumerable<string>> GetUwpFileAssociations(string filename)
1207+
{
1208+
var uwpApps = await Launcher.FindFileHandlersAsync(Path.GetExtension(filename));
1209+
return uwpApps.Select(x => x.PackageFamilyName);
1210+
}
1211+
1212+
private static string? GetDesktopFileAssociation(string filename)
1213+
{
1214+
var lpResult = new StringBuilder(2048);
1215+
var hResult = Shell32.FindExecutable(filename, null, lpResult);
1216+
return hResult.ToInt64() > 32 ? lpResult.ToString() : null;
1217+
}
12131218
}
12141219
}

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)