728x90
반응형
728x170
▶ Uri 클래스 : TryCreate 정적 메소드를 사용해 URL에서 파일명 구하기 예제
Console.WriteLine(GetFileName("")); // ""
Console.WriteLine(GetFileName("test")); // "test"
Console.WriteLine(GetFileName("test.xml")); // "test.xml"
Console.WriteLine(GetFileName("/test.xml")); // "test.xml"
Console.WriteLine(GetFileName("/test.xml?q=1")); // "test.xml"
Console.WriteLine(GetFileName("/test.xml?q=1&x=3")); // "test.xml"
Console.WriteLine(GetFileName("test.xml?q=1&x=3")); // "test.xml"
Console.WriteLine(GetFileName("http://www.a.com/test.xml?q=1&x=3")); // "test.xml"
Console.WriteLine(GetFileName("http://www.a.com/test.xml?q=1&x=3#aidjsf")); // "test.xml"
Console.WriteLine(GetFileName("http://www.a.com/a/b/c/d")); // "d"
Console.WriteLine(GetFileName("http://www.a.com/a/b/c/d/e/")); // ""
300x250
▶ Uri 클래스 : TryCreate 정적 메소드를 사용해 URL에서 파일명 구하기
/// <summary>
/// 테스트 URI
/// </summary>
private readonly static Uri _testURI = new Uri("http://canbeanything");
#region 파일명 구하기 - GetFileName(url)
/// <summary>
/// 파일명 구하기
/// </summary>
/// <param name="url">URL</param>
/// <returns>파일명</returns>
public string GetFileName(string url)
{
if(!Uri.TryCreate(url, UriKind.Absolute, out Uri uri))
{
uri = new Uri(_testURI, url);
}
return Path.GetFileName(uri.LocalPath);
}
#endregion
728x90
반응형
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON/.NET6] 배열 병합하기 (0) | 2022.07.28 |
---|---|
[C#/COMMON/.NET6] Array 클래스 : CopyTo 메소드를 사용해 배열 병합하기 (0) | 2022.07.28 |
[C#/COMMON] 트리 데이터 구조 사용하기 (0) | 2022.07.28 |
[C#/COMMON/.NET6] List<T> 클래스 : ConvertAll 메소드를 사용해 정수 문자열 리스트에서 정수 리스트 구하기 (0) | 2022.07.28 |
[C#/COMMON/.NET6] Uri 클래스 : TryCreate 정적 메소드를 사용해 URL에서 파일명 구하기 (0) | 2022.07.27 |
[C#/COMMON/.NET6] Uri 클래스 : IsFile 속성을 사용해 URI에서 파일명 구하기 (0) | 2022.07.27 |
[C#/COMMON/.NET6] Array 클래스 : GetLength 메소드를 사용해 다차원 배열 크기 구하기 (0) | 2022.07.27 |
[C#/COMMON/.NET6] 재시도 로직 사용하기 (0) | 2022.07.27 |
[C#/COMMON/.NET6] DateTime 구조체 : ParseExact 메소드를 사용해 DateTime 구하기 (0) | 2022.07.22 |
[C#/COMMON/.NET6] 참조 어셈블리 로드하기 (0) | 2022.07.20 |
댓글을 달아 주세요