6

ファイルの保存ダイアログがあり、入力したファイル名のみを取得したい。に相当

    openfiledialog.SafeFileName;

Save file dialog has no SafeFileName Property and FileName returns both filename, path and extension. Pls how do i extract only file name.

4

2 に答える 2

18

拡張子付きのファイル名が必要な場合は、Path.GetFileName(). 拡張子なしでも必要な場合は、 を使用してPath.GetFileNameWithoutExtension()ください。

public void Test(string fileName)
{
    string path = Path.GetDirectoryName(fileName);
    string filename_with_ext = Path.GetFileName(fileName);
    string filename_without_ext = Path.GetFileNameWithoutExtension(fileName);
    string ext_only = Path.GetExtension(fileName);
}

詳細、特にPath多くの便利なメソッドを持つクラスについては、MSDN を参照してください。

http://msdn.microsoft.com/en-us/library/System.IO.Path_methods.aspx

http://msdn.microsoft.com/en-us/library/system.io.path.getfilename.aspx

http://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension.aspx

于 2013-08-14T21:18:05.437 に答える