2019/09/17 11:39:28
/////////////////////////////////////////////////////////////////////////
/// <summary>
/// function:ファイル名の短縮表示の取得2
/// </summary>
/// <param name="str_path"></param>
/// <returns></returns>
public static string getShortenFilePath2( string str_path )
{
// v0.98.0033 add
if ( str_path.Trim().Equals( "" ) ) return " ";
string shorten_path = "";
// ドライブ文字をピリオドに置き換え
if ( ':'.Equals( str_path[ 1 ] ) )
{
shorten_path = "." + str_path.Substring( 1 );
}
// ユーザーフォルダ名をピリオドに置き換え
// :\user\ の次の文字列を。
int idx = shorten_path.IndexOf( ":\\Users\\" );
if ( 0 < idx )
{
idx += 7;
int idx2 = shorten_path.IndexOf( "\\", idx + 1 );
shorten_path = shorten_path.Substring( 0, idx )
+ "\\..."
+ shorten_path.Substring( idx2 );
idx = shorten_path.IndexOf( "\\", idx + 1 );
}
else
{
// v0.98.0041
idx = 2;
}
// 上記のあと、フォルダがいくつあるか?
int num_folder = 0;
int idx3 = idx;
while ( 0 < ( idx3 = shorten_path.IndexOf( "\\", idx3 + 1 ) ) )
{
num_folder++;
};
// 残りのフォルダが2つ以上なら、直上だけを残して短縮表示
if ( 2 <= num_folder )
{
// 短縮文字列の先頭部分
//string s_tmp = shorten_path.Substring( 0, idx );
StringBuilder sb_path = new StringBuilder( shorten_path.Substring( 0, idx ) );
// ( num_folder-1 は、フォルダを1つ残すため)
for ( int i = 1; i <= num_folder - 1; i++ )
{
// 加工しない部分を決める
idx = shorten_path.IndexOf( "\\", idx + 1 );
// 短縮文字列の組立て
//s_tmp += "\\...";
sb_path.Append( "\\..." );
}
// 短縮表示文字列に、フォルダ+ファイル名を加える。
//shorten_path = s_tmp + shorten_path.Substring( idx );
sb_path.Append( shorten_path.Substring( idx ) );
shorten_path = sb_path.ToString();
}
// debug
// Console.WriteLine( "num_folder=" + num_folder );
return shorten_path;
}