\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

FDK.フォルダ クラスで、path が null のときに例外が発生していたミスを修正。
[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( null == 変換したいフォルダの絶対パス )
13                                 return 変換したいフォルダの絶対パス;
14                         if( false == Path.IsPathRooted( 基点フォルダの絶対パス ) )
15                                 throw new FDKException( $"指定された基点フォルダが絶対パスではありません。[{基点フォルダの絶対パス}]" );
16                         if( false == Path.IsPathRooted( 変換したいフォルダの絶対パス ) )
17                                 throw new FDKException( $"指定された変換対象フォルダが絶対パスではありません。[{変換したいフォルダの絶対パス}]" );
18
19                         if( '\\' != 基点フォルダの絶対パス[ 基点フォルダの絶対パス.Length - 1 ] )
20                                 基点フォルダの絶対パス += @"\";    // 末尾は必ず \ にする
21
22                         // 絶対-相対パス変換は、Path ではなく Uri でしか行えない。
23                         var 基点uri = new Uri( 基点フォルダの絶対パス );
24                         var 変換前uri = new Uri( 変換したいフォルダの絶対パス );
25                         var 変換後uri = 基点uri.MakeRelativeUri( 変換前uri );
26
27                         // Uri をデコードして返す。'/' は '\' に置換する。
28                         return Uri.UnescapeDataString( 変換後uri.ToString() ).Replace( oldChar: '/', newChar: '\\' );
29                 }
30
31                 public static void フォルダ変数を追加する( string 変数名, string 置換するパス文字列 )
32                 {
33                         フォルダ.フォルダ変数toパス[ 変数名 ] = 置換するパス文字列;
34                 }
35
36                 public static void フォルダ変数を削除する( string 変数名 )
37                 {
38                         if( フォルダ.フォルダ変数toパス.ContainsKey( 変数名 ) )
39                                 フォルダ.フォルダ変数toパス.Remove( 変数名 );
40                         else
41                                 throw new FDKException( $"指定されたフォルダ変数「{変数名}」は存在しません。" );
42                 }
43
44                 public static string 絶対パスに含まれるフォルダ変数を展開して返す( string path )
45                 {
46                         if( null != path )
47                         {
48                                 foreach( var kvp in フォルダ.フォルダ変数toパス )
49                                 {
50                                         if( kvp.Value.Nullまたは空である() )
51                                                 continue;
52                                         path = path.Replace( "$(" + kvp.Key + ")", kvp.Value );
53                                 }
54                         }
55                         return path;
56                 }
57
58                 public static string 絶対パスをフォルダ変数付き絶対パスに変換して返す( string path )
59                 {
60                         if( null != path )
61                         {
62                                 foreach( var kvp in フォルダ.フォルダ変数toパス )
63                                 {
64                                         if( kvp.Value.Nullまたは空である() )
65                                                 continue;
66                                         path = path.Replace( kvp.Value, "$(" + kvp.Key + ")" );
67                                 }
68                         }
69                         return path;
70                 }
71
72                 protected static readonly Dictionary<string, string> フォルダ変数toパス = new Dictionary<string, string>();
73         }
74 }