[Tips][パス操作] パス文字列から拡張子を除いたファイル名を取得する

スポンサーリンク

パス文字列から拡張子を除いたファイル名を取得するには、PathクラスGetFileNameWithoutExtensionメソッドを使用します。

下記はパス文字列から拡張子を除いたファイル名を取得するです。

“C:\Work\Test.txt”からファイル名部分の”Test”を抜き出して表示します。

VBの例

Dim filepath As String = "C:\Work\Test.txt"

'パス文字列から拡張子を除いたファイル名を取得する
Dim strFilename As String = IO.Path.GetFileNameWithoutExtension(filepath)

MessageBox.Show(strFilename)

C#の例

string filepath = @"C:\Work\Test.txt";

// パス文字列から拡張子を除いたファイル名を取得する
string strFilename = System.IO.Path.GetFileNameWithoutExtension(filepath);

MessageBox.Show(strFilename);
Please follow and like us:

コメント

タイトルとURLをコピーしました