Zaarinn
Zaarinn

Reputation: 13

Retrieving the File Name of a .mp3 file without the path

Reasoning:

I've seen this question duplicated a few times on this website, and each one seems to bring people to same conclusion.

Okay, so I have a music player located within a window. When the window opens, an OpenFileDialog method is ran, to where the user must pick a .mp3 file. Once done, they can play the file whilst navigating other windows in the program.

I'm trying to:

Enter the file name of the selected file into a label. With this, I want to show the file name as it is being played. I want to only show the file name, not the path to it as well.

What I've seen on duplicate questions:

Repeating fixes whereby I can use the following code to pick up the file name with or without the file extension:

label1.Content = Path.GetFileName(media.Source.ToString());

label1.Content = Path.GetFileNameWithoutExtension(media.Source.ToString());

Issue:

I've tried to enter this on my program, but I don't have the "Path" section (I'm not too sure of the actual name of this part of code).

The closest I can get to this is: class System.Windows.Shapes.Path | Draws a series of connected lines and curves.

I don't seem to have another "path" in my VS, so I'm not too sure what to do.

Code (Sorry about the code here, it was a little awkward to copy into the question box)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Win32;
using System.Windows.Threading;

namespace WolfAlbumCatalogue
{
    /// <summary>
    /// Interaction logic for MusicPlayer.xaml
    /// </summary>
    public partial class MusicPlayer : Window
    {
        private MediaPlayer mediaPlayer = new MediaPlayer();

        AlbumCatalogue albumCatalogue;

        public MusicPlayer(AlbumCatalogue parent)
        {
            InitializeComponent();
            albumCatalogue = parent;

            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "MP3 files (*.mp3)|*.mp3|All files(*.*)|*.*";
            if (openFileDialog.ShowDialog() == true)
                mediaPlayer.Open(new Uri(openFileDialog.FileName));

            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += timer_Tick;
            timer.Tick += miniTimer_Tick;
            timer.Start();

            albumCatalogue.img_musicPlayerWindow.IsEnabled = false;

            lbl_songName.Content = Path
        }

        private void rec_closeHover_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            this.Close();
            albumCatalogue.img_musicPlayerWindow.IsEnabled = true;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            if (mediaPlayer.Source != null)
            {
                lbl_timer.Content = String.Format("{0} / {1}", mediaPlayer.Position.ToString(@"mm\:ss"), mediaPlayer.NaturalDuration.TimeSpan.ToString(@"mm\:ss"));
            }
            else
            {
                lbl_timer.Content = "No file selected...";
            }
        }

        void miniTimer_Tick(object sender, EventArgs e)
        {
            if (mediaPlayer.Source != null)
            {
                lbl_miniTimer.Content = String.Format("{0} / {1}", mediaPlayer.Position.ToString(@"mm\:ss"), mediaPlayer.NaturalDuration.TimeSpan.ToString(@"mm\:ss"));
            }
            else
            {
                lbl_miniTimer.Content = "No file selected...";
            }
        }

        private void btn_play_Click(object sender, RoutedEventArgs e)
        {
            mediaPlayer.Play();
            lbl_play_pause.Content = "Playing...";
            lbl_play_pause.Visibility = Visibility.Visible;
        }

        private void btn_pause_Click(object sender, RoutedEventArgs e)
        {
            mediaPlayer.Pause();
            lbl_play_pause.Content = "Paused.";
            lbl_play_pause.Visibility = Visibility.Visible;
        }

        private void btn_stop_Click(object sender, RoutedEventArgs e)
        {
            mediaPlayer.Stop();
            lbl_play_pause.Visibility = Visibility.Hidden;
        }

        private void rec_titleBar_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void rec_windowBkg_MouseDown(object sender, MouseButtonEventArgs e)
        {
        this.DragMove();
        }

        private void rec_btnOpenAudioFile_MouseDown(object sender, MouseButtonEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "MP3 files (*.mp3)|*.mp3|All files(*.*)|*.*";
            if (openFileDialog.ShowDialog() == true)
                mediaPlayer.Open(new Uri(openFileDialog.FileName));

            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(1);
            timer.Tick += timer_Tick;
            timer.Start();
        }
    }
}

I would copy in the XAML too but I use blend animations and it's way too long for this question. If you need it, please let me know :)

That said, I think that's everything!

Thank you

Upvotes: 0

Views: 410

Answers (1)

reshmi k
reshmi k

Reputation: 46

Include the header System.IO in your application. (using System.IO;) .If it is not found you need to add the reference System dll.

Upvotes: 1

Related Questions