C#不能隐式转换类型';System.IO.FileInfo';串

C#不能隐式转换类型';System.IO.FileInfo';串,c#,type-conversion,windows-media-player,C#,Type Conversion,Windows Media Player,我的列表框中有一个音乐文件列表,当我双击它们时,我正试图使用媒体播放器播放它们。但它不断地出现错误。我假设这是因为“文件”不是字符串,但如何将其转换为字符串?我试过使用.ToString,但不起作用。我对这个很陌生。 感谢您的帮助 错误出现在axWindowsMediaPlayer.URL=文件[listBox1.SelectedIndex]中 这是我的密码: public partial class Form1 : Form { public Form1() {

我的列表框中有一个音乐文件列表,当我双击它们时,我正试图使用媒体播放器播放它们。但它不断地出现错误。我假设这是因为“文件”不是字符串,但如何将其转换为字符串?我试过使用.ToString,但不起作用。我对这个很陌生。 感谢您的帮助

错误出现在axWindowsMediaPlayer.URL=文件[listBox1.SelectedIndex]中

这是我的密码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    string[] extensions = new[] { ".mp3", ".wma", ".wav", ".MP3", ".WMA" };

    FileInfo[] files;

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex];
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        DirectoryInfo dinfo = new DirectoryInfo(@"C:\Tracks");
        files = dinfo.EnumerateFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();

        for (int i = 0; i < files.Length; i++)
        {
            listBox1.Items.Add(files[i]);
        }


    }

我没有打开IDE来尝试此操作,但请尝试更改:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex];
}
致:


我没有打开IDE来尝试此操作,但请尝试更改:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex];
}
致:


您需要提供AxWindowsMediaPlayer.URL属性的文件路径 您可以使用FileInfo.FullName属性获取它


因此,请使用
axWindowsMediaPlayer1.URL=files[listBox1.SelectedIndex].FullName

您需要提供AxWindowsMediaPlayer.URL属性的文件路径 您可以使用FileInfo.FullName属性获取它

因此,请使用
axWindowsMediaPlayer1.URL=files[listBox1.SelectedIndex].FullName

如果需要文件的完整路径

如果你只想知道名字

在代码中:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex].FullName;
}
如果需要文件的完整路径

如果你只想知道名字

在代码中:

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    axWindowsMediaPlayer1.URL = files[listBox1.SelectedIndex].FullName;
}

dinfo.EnumerateFiles
返回
FileInfo
对象的集合,您可以将其填充到列表框中。那么,谜团是什么呢?您能准确地发布您尝试过但不起作用的代码吗?这可能是因为您正在查找该实例的
FullName
属性。我不确定更改后您的cose是否可以工作,因为FullName不包含URL。但也许你很幸运。至少您修复了编译错误。“如何将其转换为字符串?”-这取决于您希望该字符串是什么。您有一个
FileInfo
对象。当您将该对象显示为字符串时,您希望它显示什么?
dinfo.EnumerateFiles
返回
FileInfo
对象的集合,并将其填充到列表框中。那么,谜团是什么呢?您能准确地发布您尝试过但不起作用的代码吗?这可能是因为您正在查找该实例的
FullName
属性。我不确定更改后您的cose是否可以工作,因为FullName不包含URL。但也许你很幸运。至少您修复了编译错误。“如何将其转换为字符串?”-这取决于您希望该字符串是什么。您有一个
FileInfo
对象。当您将该对象显示为字符串时,您希望它显示什么?