Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# FileDialogue-显然它没有';不行?_C#_Mp3 - Fatal编程技术网

C# FileDialogue-显然它没有';不行?

C# FileDialogue-显然它没有';不行?,c#,mp3,C#,Mp3,我目前正试图为一个项目制作我的mp3播放器 我通过以下指南编写了此代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

我目前正试图为一个项目制作我的mp3播放器

我通过以下指南编写了此代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }    
        string[] files, path;
        [STAThread]  
        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                files = openFileDialog1.SafeFileNames;
                path = openFileDialog1.FileNames;
                for (int i = 0; i < files.Length; i++)
                {
                    listBox1.Items.Add(files[i]);
                }
            }
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            axWindowsMediaPlayer1.URL = path[listBox1.SelectedIndex];
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间Windows窗体应用程序2
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}    
字符串[]文件,路径;
[状态线程]
私有无效按钮1\u单击(对象发送者,事件参数e)
{
if(openFileDialog1.ShowDialog()==System.Windows.Forms.DialogResult.OK)
{
files=openFileDialog1.safefilename;
path=openFileDialog1.FileNames;
for(int i=0;i
这是我表格的屏幕:

由于某种原因,当我单击“打开”按钮时,什么也没有发生。首先,当我使用这段代码时,我得到了一个关于“Form1_load”的错误,但由于我没有使用它,我只是删除了错误行,然后没有发现错误

我很笨,所以有人知道怎么回事吗

我完全遵循了这个指南:


谢谢

首先,请确保您已经为按钮分配了单击事件,只需从设计窗口双击按钮,如果它将您重定向到按钮单击方法,则一切正常,如果它没有,则首先分配它。为此

 public Form1()
        {
            InitializeComponent();
            button1.Click += button1_Click;
        } 

为什么需要[StatThread]在这里?我的意思是只需删除该属性并重试,因为[StatThread]用于以下原因:-

stathread属性标记要使用单线程COM的线程 如果需要COM,请选择公寓。默认情况下,.NET不会在初始化COM 全部的仅当需要COM时,如COM对象或COM 控件,或者当需要拖放时,该COM 初始化。当这种情况发生时,.NET调用底层 CoInitializeEx函数,它使用指示是否加入的标志 多线程或单线程单元的线程

有关更多信息:-


粘贴“打开的文件”的代码button@andy“打开的文件”的ID可能为
button1
您是否将单击事件分配给了按钮?这非常有效!不,我有另一个问题,我可以选择我的mp3文件,但我不能播放它?