Powershell 은 C\#를 사용 하여 줄 임 말 경 로 를 실현 합 니 다.

2.0 및 이후 버 전 지원.
어떤 때 는 보고서 의 경로 문자열 이 매우 길다.필요 하 다 면 줄 일 수도 있 지만,이 경 로 는 사용 가 치 를 잃 게 됩 니 다.내 장 된 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
이 코드 를 실행 하면 새로운.Net 클래스 가 생 성 됩 니 다.그 중에서 새로운 정적 방법 인'CompactPath'가 추 가 됩 니 다.이제 이렇게 사용 할 수 있 습 니 다.

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

좋은 웹페이지 즐겨찾기