DanysysTeam / PS-SFTA

PowerShell Set File Type Association
253 stars 53 forks source link

Set-FTA flashes all icons on the desktop. #34

Open itoc-dw opened 3 months ago

itoc-dw commented 3 months ago

Hi, This project has been very useful for our team, so thank you sharing.

We had various customers on Azure Virtual Desktop reporting that their screen constantly flashes each time Set-FTA is run. To limit the flashing, we altered the code to within Set-FTA to only execute if it differs by using Get-FTA which is significantly quicker as well when using the measure-command.

Not sure if it breaks it for any other functions as we don't use all functionality but thought I might share it if its useful.


#### Set-FTA causes flashes of icons on the desktop when it executed. Get-FTA does not have this problem
#### Get-FTA takes 5-10ms. Set-FTA takes 500ms.

function Set-FTA {

  [CmdletBinding()]
  param (
    [Parameter(Mandatory = $true)]
    [String]
    $ProgId,

    [Parameter(Mandatory = $true)]
    [Alias("Protocol")]
    [String]
    $Extension,

    [String]
    $Icon
  )

  if (Test-Path -Path $ProgId) {
    $ProgId = "SFTA." + [System.IO.Path]::GetFileNameWithoutExtension($ProgId).replace(" ", "") + $Extension
  }

  Write-Verbose "ProgId: $ProgId"
  Write-Verbose "Extension/Protocol: $Extension"

  #### START NEW CODE
  try {
    if ((Get-FTA -Extension $Extension -ErrorAction Stop) -eq $ProgId) { 
        Write-Verbose "Extension $Extension is already set to $ProgId"
        return 
    }
    else {
        Write-Output "Extension $Extension required to be set to $ProgId"
    }
  } 
  catch {
    Write-Verbose "Unable to determine Get-FTA on SET-FTA "
  }
  #### END NEW CODE

  function local:Update-RegistryChanges {
    $code = @'
    [System.Runtime.InteropServices.DllImport("Shell32.dll")] 
    private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);
    public static void Refresh() {
        SHChangeNotify(0x8000000, 0, IntPtr.Zero, IntPtr.Zero);    
    }
'@ 

    try {
      Add-Type -MemberDefinition $code -Namespace SHChange -Name Notify
    }
    catch {}

    try {
      [SHChange.Notify]::Refresh()
    }
    catch {} 
  }

##### TRUNCATED