openfiledialog文件和目录?

时间:2012-12-16 17:15:01

标签: c# openfiledialog

我正在尝试制作简单的mp3播放器。当我使用此

 listBox2.Items.Add(openFileDialog1.FileName);

将歌曲添加到我的列表框中它正在工作,但它显示文件目录,所以我改变了这样

listBox2.Items.Add(openFileDialog1.SafeFileName);

然后它看起来在listbox1上的歌曲名称但是当我点击播放按钮时它不起作用:(

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication12222
{
    public partial class Form1 : Form
    {
        [DllImport("winmm.dll")]
        private static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
        public string Pcommand;
        public bool isOpen;
        public Form1()
        {
            InitializeComponent();
        }

        public void Stop()
        {
            Pcommand = "close MediaFile";
            mciSendString(Pcommand, null, 0, IntPtr.Zero);
            isOpen = false;
        }

        public void Start()
        {
            Pcommand = "open \"" + listBox1.SelectedItem + "\" type mpegvideo alias MediaFile";
            mciSendString(Pcommand, null, 0, IntPtr.Zero);
            isOpen = true;
            Play(true);
        }

        private void button10_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "Media File(*.mpg,*.dat,*.avi,*.wmv,*.wav,*.mp3)|*.wav;*.mp3;*.mpg;*.dat;*.avi;*.wmv";
            openFileDialog1.ShowDialog();

            if (openFileDialog1.FileName != ""){

               // listBox1.Items.Add(openFileDialog1.SafeFileName);

                listBox2.Items.Add(openFileDialog1.FileName);
            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Start();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Stop();
        }
        public void Play(bool loop)
        {
            if (isOpen)
            {
                Pcommand = "play MediaFile";
                if (loop)
                    Pcommand += " REPEAT";
                mciSendString(Pcommand, null, 0, IntPtr.Zero);
            }
        }
        int x;
        private void button2_Click(object sender, EventArgs e)
        {

            if (listBox1.SelectedIndex ==listBox1.Items.Count-1 ) { x++; }
            else
            {
                Stop();
                listBox1.SelectedIndex = listBox1.SelectedIndex + 1;

                Start();
            }
        }
        int y;
        private void button5_Click(object sender, EventArgs e)
        {

            if (listBox1.SelectedIndex ==0) { y++; }
            else
            {
                Stop();
                listBox1.SelectedIndex = listBox1.SelectedIndex - 1;

                Start();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.SelectedIndex = 0;

            Stop();
            Start();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            listBox1.SelectedIndex = listBox1.Items.Count - 1;
            Stop();
            Start();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            Pcommand = "close MediaFile";
            mciSendString(Pcommand, null, 0, IntPtr.Zero);
            isOpen = false;
        }

        private void button8_Click(object sender, EventArgs e)
        {

        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

    }
}

额外的问题是可以混合(随机)列表框。我在mp3播放器上添加歌曲然后当我想点击混合按钮我希望列表混合。是否有任何listbox命令?

2 个答案:

答案 0 :(得分:2)

创建自定义类:

public class FileItem
{
    public string FilePath { get; set; }
    public string ShortName { get; set; }
}

然后在从OpenFileDialog获取文件时创建此类的新实例,将openFileDialog1.FileName保存到FilePath属性,然后使用{获取短文件名{1}}方法。

不是直接从Windows.IO.Path.GetFileName()ListBox添加文件路径,而是添加此类的实例。

并将OpenFileDialog的{​​{1}}属性更改为DisplayMember,这样文件路径的“短名称”就会显示在ListBox中。

答案 1 :(得分:1)

您应该能够使用以下链接随机播放您的播放列表。

Random playlist algorithm

谢谢, 海军