Home > C# > C# 学び直し > ファイル IO > パス操作

パス名を分解する

新規作成日 2019-02-05
最終更新日

読み込んだテキストファイルの名前に、処理済みであることを示す文字列を付加して、保存したい場面に出会うことがあります。

ファイル名は、パスとファイル名、拡張子をあわせて取得し、使用します。それぞれを別個に利用したい場合は、別個に取り出す操作が必要です。そして、それぞれを別個に扱う方法は、あらかじめ、用意されています。

パスを構成するそれぞの要素は、パス構成要素と呼ばれ、Pathクラスで、必要な部分だけを取得することができます。

サンプルコード

MainWindow.xaml

<Window x:Class="PassOperation01.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="200" Width="400">
    <StackPanel>
        <TextBox Name="fullPath" TextChanged="FullPath_TextChanged"></TextBox>
        <Button Click="Button_Click">ファイル名指定</Button>
        <TextBlock Name="directryName"></TextBlock>
        <TextBlock Name="fileName"></TextBlock>
        <TextBlock Name="extention"></TextBlock>
        <TextBlock Name="fullPassWithoutExt"></TextBlock>
        <TextBlock Name="rootPath"></TextBlock>
    </StackPanel>
</Window>

MainWindow.xaml.cs

using Microsoft.Win32;
using System.Windows;
using System.Windows.Controls;


namespace PassOperation01
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Configure open file dialog box オープンファイル・ダイアログボックスを設定します。
            OpenFileDialog dlg = new OpenFileDialog();

            // Show open file dialog box オープンファイル・ダイアログボックスを表示します。
            bool? result = dlg.ShowDialog();

            // Process open file dialog box results オープンファイル・ダイアログボックスの結果を処理します。
            if (result == true)
            {
                // ファイル名を取得します。
                string filename = dlg.FileName;
                // TextBoxにファイル名を表示します。
                fullPath.Text = filename;
            }
        }

        private void FullPath_TextChanged(object sender, TextChangedEventArgs e)
        {
            string targetPath = fullPath.Text;

            // 指定したパス文字列のディレクトリ情報を返す。末尾には、\が含まれない。
            directryName.Text = System.IO.Path.GetDirectoryName(targetPath);
            // 指定したパス文字列のファイル名と拡張子を返す
            fileName.Text = System.IO.Path.GetFileName(targetPath);
            // 指定したパス文字列の拡張子を返す。返される拡張子は、.(ピリオド)を含む
            extention.Text = System.IO.Path.GetExtension(targetPath);
            // 指定したパス文字列のファイル名を拡張子をつけずに返す。
            fullPassWithoutExt.Text = System.IO.Path.GetFileNameWithoutExtension(targetPath);
            // 指定したパス文字列のルートディレクトリ情報("C:\"など)を返す。
            rootPath.Text = System.IO.Path.GetPathRoot(targetPath);
        }
    }
}

サンプルコードの操作説明

実行されると表示されるウィンドウ

実行するとウィンドウが表示されます。

「ファイル名指定」を選択し、ファイル・オープン・ダイアログを使用して、ファイルを選択します。

パスが、さまざまな部分に別れて、表示されます。

パスが、さまざまな部分に別れて、表示されます。

参考サイト

このエントリーをはてなブックマークに追加

Home PC C# Illustration

Copyright (C) 2011 Horio Kazuhiko(kukekko) All Rights Reserved.
kukekko@gmail.com
ご連絡の際は、お問い合わせページのURLの明記をお願いします。
「掲載内容は私自身の見解であり、所属する組織を代表するものではありません。」