Skip to content

Commit 9ee8447

Browse files
authored
Fix: Fixed an issue where changing icons didn't work when admin access was required (#15625)
1 parent 6bd707a commit 9ee8447

File tree

1 file changed

+38
-7
lines changed

1 file changed

+38
-7
lines changed

src/Files.App/Utils/Storage/Operations/FileOperationsHelpers.cs

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Files.App.Utils.Storage.Operations;
55
using Files.Shared.Helpers;
66
using Microsoft.Extensions.Logging;
7-
using Microsoft.Win32;
87
using System.Collections.Concurrent;
98
using System.IO;
109
using System.Runtime.InteropServices;
@@ -492,7 +491,7 @@ public static Task SetClipboard(string[] filesToCopy, DataPackageOperation opera
492491
Destination = e.DestFolder.GetParsingPath() is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(e.DestFolder.GetParsingPath(), e.Name) : null,
493492
HResult = (int)e.Result
494493
});
495-
494+
496495
UpdateFileTagsDb(e, "move");
497496
};
498497

@@ -580,10 +579,10 @@ public static Task SetClipboard(string[] filesToCopy, DataPackageOperation opera
580579
{
581580
using ShellItem shi = new(fileToCopyPath[i]);
582581
using ShellFolder shd = new(Path.GetDirectoryName(copyDestination[i]));
583-
582+
584583
var fileName = GetIncrementalName(overwriteOnCopy, copyDestination[i], fileToCopyPath[i]);
585584
// Perform a copy operation
586-
op.QueueCopyOperation(shi, shd, fileName);
585+
op.QueueCopyOperation(shi, shd, fileName);
587586
}))
588587
{
589588
shellOperationResult.Items.Add(new ShellOperationItemResult()
@@ -623,7 +622,7 @@ public static Task SetClipboard(string[] filesToCopy, DataPackageOperation opera
623622
Destination = e.DestFolder.GetParsingPath() is not null && !string.IsNullOrEmpty(e.Name) ? Path.Combine(e.DestFolder.GetParsingPath(), e.Name) : null,
624623
HResult = (int)e.Result
625624
});
626-
625+
627626
UpdateFileTagsDb(e, "copy");
628627
};
629628

@@ -774,6 +773,38 @@ public static bool SetLinkIcon(string filePath, string iconFile, int iconIndex)
774773
link.SaveAs(filePath); // Overwrite if exists
775774
return true;
776775
}
776+
catch (UnauthorizedAccessException)
777+
{
778+
string psScript = $@"
779+
$FilePath = '{filePath}'
780+
$IconFile = '{iconFile}'
781+
$IconIndex = '{iconIndex}'
782+
783+
$Shell = New-Object -ComObject WScript.Shell
784+
$Shortcut = $Shell.CreateShortcut($FilePath)
785+
$Shortcut.IconLocation = ""$IconFile, $IconIndex""
786+
$Shortcut.Save()
787+
";
788+
789+
var base64EncodedScript = Convert.ToBase64String(System.Text.Encoding.Unicode.GetBytes(psScript));
790+
791+
ProcessStartInfo startInfo = new ProcessStartInfo()
792+
{
793+
FileName = "powershell.exe",
794+
Arguments = $"-NoProfile -EncodedCommand {base64EncodedScript}",
795+
Verb = "runas",
796+
CreateNoWindow = true,
797+
WindowStyle = ProcessWindowStyle.Hidden,
798+
UseShellExecute = true
799+
};
800+
801+
// Start the process
802+
Process process = new Process() { StartInfo = startInfo };
803+
process.Start();
804+
process.WaitForExit();
805+
806+
return true;
807+
}
777808
catch (Exception ex)
778809
{
779810
// Could not create shortcut
@@ -1023,7 +1054,7 @@ protected override void Dispose(bool disposing)
10231054
private static string GetIncrementalName(bool overWriteOnCopy, string? filePathToCheck, string? filePathToCopy)
10241055
{
10251056
if (filePathToCheck == null)
1026-
return null;
1057+
return null;
10271058

10281059
if ((!Path.Exists(filePathToCheck)) || overWriteOnCopy || filePathToCheck == filePathToCopy)
10291060
return Path.GetFileName(filePathToCheck);
@@ -1039,6 +1070,6 @@ private static string GetIncrementalName(bool overWriteOnCopy, string? filePathT
10391070
index++;
10401071

10411072
return Path.GetFileName(genFilePath(index));
1042-
}
1073+
}
10431074
}
10441075
}

0 commit comments

Comments
 (0)