\83\95ã\82©ã\83«ã\83\80.cs - RSS feed" href="/view?p=strokestylet/CsWin10Desktop3.git;a=rss;f=FDK24/%C3%A3%C2%83%C2%95%C3%A3%C2%82%C2%A9%C3%A3%C2%83%C2%AB%C3%A3%C2%83%C2%80.cs" type="application/rss+xml" /> \83\95ã\82©ã\83«ã\83\80.cs - RSS feed (no merges)" href="/view?p=strokestylet/CsWin10Desktop3.git;a=rss;f=FDK24/%C3%A3%C2%83%C2%95%C3%A3%C2%82%C2%A9%C3%A3%C2%83%C2%AB%C3%A3%C2%83%C2%80.cs;opt=--no-merges" type="application/rss+xml" /> \83\95ã\82©ã\83«ã\83\80.cs - Atom feed" href="/view?p=strokestylet/CsWin10Desktop3.git;a=atom;f=FDK24/%C3%A3%C2%83%C2%95%C3%A3%C2%82%C2%A9%C3%A3%C2%83%C2%AB%C3%A3%C2%83%C2%80.cs" type="application/atom+xml" /> \83\95ã\82©ã\83«ã\83\80.cs - Atom feed (no merges)" href="/view?p=strokestylet/CsWin10Desktop3.git;a=atom;f=FDK24/%C3%A3%C2%83%C2%95%C3%A3%C2%82%C2%A9%C3%A3%C2%83%C2%AB%C3%A3%C2%83%C2%80.cs;opt=--no-merges" type="application/atom+xml" />

OSDN Git Service

Merge branch 'ビュアーモード' into develop
[strokestylet/CsWin10Desktop3.git] / FDK24 / ã\83\95ã\82©ã\83«ã\83\80.cs
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4
5 namespace FDK
6 {
7         public class フォルダ
8         {
9                 public static string 絶対パスを相対パスに変換する( string 基点フォルダの絶対パス, string 変換したいフォルダの絶対パス )
10                 {
11                         // 引数チェック
12                         if( false == Path.IsPathRooted( 基点フォルダの絶対パス ) )
13                                 throw new FDKException( $"指定された基点フォルダが絶対パスではありません。[{基点フォルダの絶対パス}]" );
14                         if( false == Path.IsPathRooted( 変換したいフォルダの絶対パス ) )
15                                 throw new FDKException( $"指定された変換対象フォルダが絶対パスではありません。[{変換したいフォルダの絶対パス}]" );
16
17                         if( '\\' != 基点フォルダの絶対パス[ 基点フォルダの絶対パス.Length - 1 ] )
18                                 基点フォルダの絶対パス += @"\";    // 末尾は必ず \ にする
19
20                         // 絶対-相対パス変換は、Path ではなく Uri でしか行えない。
21                         var 基点uri = new Uri( 基点フォルダの絶対パス );
22                         var 変換前uri = new Uri( 変換したいフォルダの絶対パス );
23                         var 変換後uri = 基点uri.MakeRelativeUri( 変換前uri );
24
25                         // Uri をデコードして返す。'/' は '\' に置換する。
26                         return Uri.UnescapeDataString( 変換後uri.ToString() ).Replace( oldChar: '/', newChar: '\\' );
27                 }
28
29                 public static void フォルダ変数を追加する( string 変数名, string 置換するパス文字列 )
30                 {
31                         フォルダ.フォルダ変数toパス[ 変数名 ] = 置換するパス文字列;
32                 }
33                 public static void フォルダ変数を削除する( string 変数名 )
34                 {
35                         if( フォルダ.フォルダ変数toパス.ContainsKey( 変数名 ) )
36                                 フォルダ.フォルダ変数toパス.Remove( 変数名 );
37                         else
38                                 throw new FDKException( $"指定されたフォルダ変数「{変数名}」は存在しません。" );
39                 }
40                 public static string 絶対パスに含まれるフォルダ変数を展開して返す( string path )
41                 {
42                         foreach( var kvp in フォルダ.フォルダ変数toパス )
43                         {
44                                 if( kvp.Value.Nullまたは空である() )
45                                         continue;
46                                 path = path.Replace( "$(" + kvp.Key + ")", kvp.Value );
47                         }
48
49                         return path;
50                 }
51                 public static string 絶対パスをフォルダ変数付き絶対パスに変換して返す( string path )
52                 {
53                         foreach( var kvp in フォルダ.フォルダ変数toパス )
54                         {
55                                 if( kvp.Value.Nullまたは空である() )
56                                         continue;
57                                 path = path.Replace( kvp.Value, "$(" + kvp.Key + ")" );
58                         }
59
60                         return path;
61                 }
62
63                 protected static readonly Dictionary<string, string> フォルダ変数toパス = new Dictionary<string, string>();
64         }
65 }