2014-07-18 68 views
0

在PowerShell中從網絡共享驅動器訪問的時候,我無法加載圖標可例外網絡共享ExtractAssociatedIcon使用PowerShell

$IconPath = $pwd.Path + "\Icons\InstallIcon-F.ico" 
$HFForm.icon = [System.Drawing.Icon]::ExtractAssociatedIcon($IconPath) 

我收到此錯誤:

Exception calling "ExtractAssociatedIcon" with "1" argument(s): "The given path's format is not supported." 
$HFForm.icon = [System.Drawing.Icon]::ExtractAssociatedIcon <<<< ($IconPath) 

    + CategoryInfo   : NotSpecified: (:) [], MethodInvocationException 
    + FullyQualifiedErrorId : DotNetMethodException 
+1

'System.Drawing.Icon.ExtractAssociatedIcon()'不應該像(\\服務器\路徑)UNC路徑的工作,但我得到一個不同的錯誤使用UNC路徑:'\ \ server \ path'對於'filePath'無效'「,但它可能只是一個不同的PowerShell版本的消息,你確定$ pwd.Path是一個有效的路徑嗎?它是UNC格式嗎? – PeterK

+0

是的,我確定路徑是有效的,我檢查了相同的次數,如果我嘗試以不同的方式組合$ IconPath,我得到了「無效的」filePath'。「的異常。仍然是從網絡驅動器圖標沒有得到加載 – swatish

+0

不幸的是,這是.NET框架中的'ExtractAssociatedIcon()'方法的限制。在http: //stackoverflow.com/questions/1842226/how-to-get-the-associated-icon-from-a-network-share-file建議您使用P/Invoke作爲解決方法直接與Windows Shell API進行交互。您可以使用內聯C#導入大部分示例代碼而無需進行更改(請參閱http://technet.microsoft.com/zh-cn/library/hh849914.aspx上的Add-Type和示例)。 – PeterK

回答

0

我的測試顯示,和PeterK一樣。如果我使用驅動器盤符,但沒有映射的網絡共享不是。

我能夠通過將網絡共享映射到驅動器號來使其工作。所以:

$something = [system.drawing.icon]::extractassociatedicon("c:\windows\system32\notepad.exe") 

導致沒有錯誤。也沒有:

$something = [system.drawing.icon]::extractassociatedicon(($test.fullname)) 

與$ test.fullname只是一個映射的網絡文件路徑。

這是擴大了你的變量太多,所以我們可以看到你實際上傳遞一個好主意,因爲如果我瀏覽到網絡文件共享,擴大$ pwd.path:

Microsoft.PowerShell.Core\FileSystem::\\user-pc\users 

你幾乎肯定會通過它。所以看看。我沒有做太多有,所以我敢肯定,你可以找到「TTY」當量設置PowerShell如何格式化其顯示,只是在此期間做到這一點:

$IconPath = $pwd.Path.split('::')[2] + "\Icons\InstallIcon-F.ico" 
+0

映射網絡驅動器正在解決該問題。但我不能這樣做。在更改$ IconPath也沒有得到解決。請讓我知道我還能做些什麼來修復相同的 – swatish

+0

請讓我看看$ IconPath的值。確保它的實際輸出不是你期望的輸出。在我的回答結束時,我指出在powershell中瀏覽網絡共享會將「Microsoft.PowerShell.Core \ FileSystem ::」添加到$ pwd變量中。但是,這可能不適合你。我們不知道,直到我們看到$ IconPath的價值。 – BSAFH

0

轉換你的圖標來的Base64表示它是二進制數據,然後將其存儲在腳本本身(作爲文本)。

將其編碼爲base64後,可以使用此命令將其轉換回圖標,繞過UNC路徑問題。

$HFForm.icon = [System.Convert]::FromBase64String(' 
AAABAAkAAAAAAAEAIABbfQEAlgAAAICAAAABACAAKAgBAPF9AQBgYAAAAQAgAKiUAAAZhgIASEgA 
# 
# There will be hundreds rows depending on your icon's size. 
# 
AMADAADAAwAA4AcAAPAPAADwDwAA+A8AAPgPAAD4DwAA/B8AAPwfAAA=') 

BASE64 snippet。

#Be sure to edit the path to icon. 
# 
#Hint the result is copied to your clipboard - clip = clip.exe == google it. 

$path = "A:\R2-D2-32x32.ico" 
[convert]::ToBase64String((get-content $path -encoding byte)) | Clip 

#After running the clip command, right click paste between the quotes 

$HFForm.icon = [System.Convert]::FromBase64String('') 
+0

好吧..你可以讓我知道我怎麼可以將.ico文件轉換爲二進制數據的base64表示 – swatish

+0

是否工作? –

+0

它給我一個異常作爲異常設置「圖標」:「無法轉換值」System.Byte []「鍵入」System.Drawing.Icon「。錯誤:」參數'圖片'必須是可以使用的圖片作爲一個圖標。「」$ HFForm。 <<<< icon = [System.Convert] :: FromBase64String($ base64String) + CategoryInfo:InvalidOperation:(:) [],RuntimeException + FullyQualifiedErrorId:PropertyAssignmentException – swatish