欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Powershell使用C#實現(xiàn)縮寫路徑

 更新時間:2015年01月08日 11:05:33   投稿:junjie  
這篇文章主要介紹了Powershell使用C#實現(xiàn)縮寫路徑,縮寫路徑有時候是非常有用的,比如某些報表的路徑太長會很難看,縮寫后就會好看許多,需要的朋友可以參考下

支持2.0及以后版本。

某些時候報表中的路徑字符串是非常長的。如果需要你也可以縮寫它,但是這樣路徑就失去的使用價值。最好是使用內(nèi)置的API它可以靈活的縮略路徑。

接下來要告訴你如何在Powershell腳本中使用C#代碼:

復(fù)制代碼 代碼如下:

$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)在你就可以這樣使用它了:

復(fù)制代碼 代碼如下:

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)文章

最新評論