使用PowerShell创builddesktop.ini,以便在Windows 10上无法使用自定义文件夹图标

我想在文件夹内创builddesktop.ini文件,在子文件夹中引用一个.jpg文件,以便为父文件夹提供一个自定义图像。 这可以手动执行该过程(右键单击文件夹>属性>自定义>selectFILE)。

假设一个文件夹结构如下所示:

C:\Test ---> Folder1 ---> Fullsize ---> image.jpg ---> Folder2 ---> Fullsize ---> image.jpg 

在手动过程之前,父文件夹('Folder1')的属性是: 'Directory'

手动过程之后,该文件夹的属性为'ReadOnly, Directory' 。 “Fullsize”子文件夹保留'Directory'的原始属性。

desktop.ini文件是用像这样的内容创build的:

 [ViewState] Mode= Vid= FolderType=Generic Logo=C:\Test\Folder1\Fullsize\image.jpg 

此外,编码是UTF-8,文件属性设置为HSA( 'Hidden, System, Archive' )。

点击确定后,立即文件夹显示图像的预览。

把它变成另一个文件夹的代码,我们有:

 $folder2 = 'C:\Test\Folder2' $folder2Image = "$folder2\Fullsize\image.jpg" $ini = @" [ViewState] Mode= Vid= FolderType=Generic Logo=$Folder2Image "@ Set-ItemProperty $Folder2 -Name Attributes -Value 'ReadOnly' # Out-File creates UTF-8 with BOM so use [System.IO.File] [System.IO.File]::WriteAllLines("$Folder2\desktop.ini", $ini) $inifile = Get-Item "$Folder2\desktop.ini" $inifile.Attributes = 'Archive, System, Hidden' 

尽pipe如此,folder2将不会显示图像预览。

文件夹视图

检查Folder1和Folder2的属性(Get-Item $ path | Select Attributes)和desktop.ini文件显示它们是相同的。

除了图像的path,desktop.ini的编码和实际内容是相同的。 图像的path是正确的。 文件包含每行结尾的CRLF。 我甚至确保了图像的path是正确的(不是应该重要的)。

彻底难倒了。 手工stream程还能做什么,我失踪了?

Notepad ++中的desktop.ini文件

Windows 10:版本1607(操作系统版本14986.1001)

必须使用Shell API方法更新Desktop.ini,以便通知Shell / Explorer:

 function Set-FolderImage($folder, $imagePath) { # make a temporary folder with desktop.ini $tmpDir = (Join-Path $env:TEMP ([IO.Path]::GetRandomFileName())) mkdir $tmpDir -force >$null $tmp = "$tmpDir\desktop.ini" @" [ViewState] FolderType=Generic Logo=$imagePath "@ >$tmp (Get-Item -LiteralPath $tmp).Attributes = 'Archive, System, Hidden' # use a Shell method to move that file into the destination $shell = New-Object -com Shell.Application $shell.NameSpace($folder).MoveHere($tmp, 0x0004 + 0x0010 + 0x0400) # FOF_SILENT 0x0004 don't display progress UI # FOF_NOCONFIRMATION 0x0010 don't display confirmation UI, assume "yes" # FOF_NOERRORUI 0x0400 don't put up error UI del -LiteralPath $tmpDir -force } 

用法:

 Set-FolderImage 'R:\Temp' 'R:\33983.jpg'