支持2.0及以后版本。
某些時(shí)候報(bào)表中的路徑字符串是非常長(zhǎng)的。如果需要你也可以縮寫(xiě)它,但是這樣路徑就失去的使用價(jià)值。最好是使用內(nèi)置的API它可以靈活的縮略路徑。
接下來(lái)要告訴你如何在Powershell腳本中使用C#代碼:
代碼如下:
$newType = @'
using System;
using System.Text;
using System.Runtime.InteropServices;
namespace WindowsAPILib
{
public class Helper
{
[DllImport("shlwapi.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool PathCompactPathEx(System.Text.StringBuilder pszOut, string pszSrc, Int32 cchMax, Int32 dwFlags);
public static string CompactPath(string Path, int DesiredLength)
{
StringBuilder sb = new StringBuilder(260);
if (PathCompactPathEx(sb, Path, DesiredLength + 1, 0))
{ return sb.ToString(); }
else
{ return Path; }
}
}
}
'@
Add-Type -TypeDefinition $newType
一旦你執(zhí)行這段代碼,就會(huì)產(chǎn)生一個(gè)新的.Net類(lèi),其中會(huì)增加一個(gè)新的靜態(tài)方法“CompactPath”,現(xiàn)在你就可以這樣使用它了:
代碼如下:
PS> $pshome
C:/Windows/System32/WindowsPowerShell/v1.0
PS> [WindowsAPILib.Helper]::CompactPath($pshome, 12)
C:/W.../v1.0
PS> [WindowsAPILib.Helper]::CompactPath($pshome, 18)
C:/Windows.../v1.0
PS> [WindowsAPILib.Helper]::CompactPath($pshome, 22)
C:/Windows/Sys.../v1.0
新聞熱點(diǎn)
疑難解答
圖片精選