Skip to content

Commit 52eeed3

Browse files
Luke Blevinslukeblevins
authored andcommitted
Ensure executable files are detected properly
1 parent ece3110 commit 52eeed3

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

Files/Filesystem/ItemViewModel.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using System.Linq;
1414
using System.Runtime.CompilerServices;
1515
using System.Runtime.InteropServices;
16+
using System.Text;
1617
using System.Threading;
1718
using System.Threading.Tasks;
1819
using Windows.ApplicationModel.Core;
@@ -722,6 +723,7 @@ private async Task AddFile(StorageFile file)
722723
{
723724
var basicProperties = await file.GetBasicPropertiesAsync();
724725

726+
bool isExecutable = await IsFileExecutable(file);
725727
var itemName = file.DisplayName;
726728
var itemDate = basicProperties.DateModified;
727729
var itemPath = file.Path;
@@ -805,12 +807,23 @@ private async Task AddFile(StorageFile file)
805807
FileType = itemType,
806808
FilePath = itemPath,
807809
FileSize = itemSize,
808-
FileSizeBytes = itemSizeBytes
810+
FileSizeBytes = itemSizeBytes,
811+
IsFileExecutable = isExecutable
809812
});
810813

811814
EmptyTextState.isVisible = Visibility.Collapsed;
812815
}
813816

817+
public static async Task<bool> IsFileExecutable(StorageFile inputFile)
818+
{
819+
var firstTwoBytes = new byte[2];
820+
using(Stream stream = await inputFile.OpenStreamForReadAsync())
821+
{
822+
stream.Read(firstTwoBytes, 0, 2);
823+
}
824+
return Encoding.UTF8.GetString(firstTwoBytes) == "MZ";
825+
}
826+
814827
public async void FileContentsChanged(IStorageQueryResultBase sender, object args)
815828
{
816829
if (_filesRefreshing)

Files/Filesystem/ListedItem.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace Files.Filesystem
77
{
88
public class ListedItem
99
{
10+
public bool IsFileExecutable { get; set; }
1011
public string FolderTooltipText { get; set; }
1112
public string FolderRelativeId { get; }
1213
public Visibility FolderImg { get; set; }

Files/Interacts/Interaction.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ private async void OpenSelectedItems(bool displayApplicationPicker)
411411
currentInstance.ItemDisplayFrame.Navigate(sourcePageType, selectedItemPath, new SuppressNavigationTransitionInfo());
412412
}
413413
}
414-
else if (clickedOnItem.FileType == "Application")
414+
else if (clickedOnItem.IsFileExecutable)
415415
{
416416
// Add location to MRU List
417417
mostRecentlyUsed.Add(await StorageFile.GetFileFromPathAsync(clickedOnItem.FilePath));
@@ -438,7 +438,7 @@ private async void OpenSelectedItems(bool displayApplicationPicker)
438438
{
439439
instanceTabsView.AddNewTab(typeof(ProHome), clickedOnItem.FilePath);
440440
}
441-
else if (clickedOnItem.FileType == "Application")
441+
else if (clickedOnItem.IsFileExecutable)
442442
{
443443
// Add location to MRU List
444444
mostRecentlyUsed.Add(await StorageFile.GetFileFromPathAsync(clickedOnItem.FilePath));

Files/YourHome.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ private async void RecentsView_ItemClick(object sender, ItemClickEventArgs e)
330330
try
331331
{
332332
var file = (await StorageFile.GetFileFromPathAsync(path));
333-
if (file.DisplayType == "Application")
333+
if (await ItemViewModel.IsFileExecutable(file))
334334
{
335335
await Interaction.LaunchExe(path);
336336

0 commit comments

Comments
 (0)