C#
System.IO.Path.GetDirectoryName(ファイルパス)
System.IO.PathクラスのGetDirectoryNameメソッドを利用することでファイルパスからディレクトリ(フォルダ)名を取得することができます。
ディレクトリが存在していなくてもディレクトリ名は取得されます。
使用例
System.IO.Path.GetDirectoryName(@"C:\Users\hoge\Documents\Test.txt");
戻り値:C:\Users\hoge\Documents
System.IO.Path.GetDirectoryName(@"C:\Users\hoge\Documents\");
戻り値:C:\Users\hoge\Documents
System.IO.Path.GetDirectoryName(@"C:\Users\hoge\Documents");
戻り値:C:\Users\hoge
System.IO.Path.GetDirectoryName(@"C:\Users");
戻り値:C:\
System.IO.Path.GetDirectoryName(@"C:\");
戻り値:(null)
ルートディレクトリが指定された時にはnullが戻る
System.IO.Path.GetDirectoryName(@"..\..\Documents");
戻り値:..\..
System.IO.Path.GetDirectoryName(@"..\Documents");
戻り値:..
System.IO.Path.GetDirectoryName(@".\Documents");
戻り値:.
System.IO.Path.GetDirectoryName(@"\Documents");
戻り値:\
System.IO.Path.GetDirectoryName(@"\\192.168.1.1\hoge\Test.txt");
戻り値:\\192.168.1.1\hoge
