Powershell 은 C\#를 사용 하여 줄 임 말 경 로 를 실현 합 니 다.
1747 단어 PowershellC#줄 임 말 경로
어떤 때 는 보고서 의 경로 문자열 이 매우 길다.필요 하 다 면 줄 일 수도 있 지만,이 경 로 는 사용 가 치 를 잃 게 됩 니 다.내 장 된 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
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
(5) Powershell 별명 (Alias)예 를 들 어 Powershell 은 출력 창 을 알 아 보 는 데 사용 되 는 Clear - host 라 는 내부 함수 가 있 습 니 다.명령 프롬프트 에 cls 나 clear 명령 을 입력 하면 Powershel...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.