Powershell使用C#實現(xiàn)縮寫路徑
支持2.0及以后版本。
某些時候報表中的路徑字符串是非常長的。如果需要你也可以縮寫它,但是這樣路徑就失去的使用價值。最好是使用內(nèi)置的API它可以靈活的縮略路徑。
接下來要告訴你如何在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í)行這段代碼,就會產(chǎn)生一個新的.Net類,其中會增加一個新的靜態(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
相關(guān)文章
Powershell目錄文件夾管理權(quán)限的繼承和指定方法
這篇文章主要介紹了Powershell目錄文件夾管理權(quán)限的繼承和指定方法,本文給出了創(chuàng)建文件夾、獲取當(dāng)前權(quán)限、添加新的權(quán)限、添加管理員權(quán)限等,需要的朋友可以參考下2015-06-06
探索PowerShell(十五) 引號與轉(zhuǎn)義字符
在PowerShell中,單引號與雙引號有著不同的功能和作用。引號的使用規(guī)則往往跟變量的代換,以及特殊字符有關(guān)2012-12-12
PowerShell函數(shù)參數(shù)使用智能提示功能例子
這篇文章主要介紹了PowerShell函數(shù)參數(shù)使用智能提示功能例子,這個功能一般需要在PowerShell集成開發(fā)環(huán)境ISE中才會顯示智能提示,需要的朋友可以參考下2014-07-07

