文件信息删除命令

发布于 2024-08-22 15:56:40 字数 2223 浏览 4 评论 0原文

我的应用程序执行此操作,使用 wmplib 播放选定的声音,并在 statechangeevent = 1(媒体播放器停止)时,我将使用 close() 命令关闭播放器并将 URL 设置为“”;

现在每次我尝试删除当前正在播放的声音时,实际上它都会被删除,因为当我查看目录时,文件已被删除。每次删除文件后,我都会重新加载控件,其中将列出目录中的媒体文件。但是,它仍然会在目录中找到已删除的声音。

我尝试过这种方式,慢慢调试,我发现 File info delete() 命令需要更长的时间来删除,因为关闭媒体播放器需要更长的时间才能删除删除,这就是为什么每当我在删除当前播放的歌曲后尝试重新加载收藏集时,已删除的歌曲仍然存在。 当我在媒体播放器未播放歌曲时尝试删除时,它删除得很快,并且重新加载集合确实显示歌曲消失的结果。

问题是,如何删除当前正在播放的文件,并确保重新加载收藏时会显示目录中正确的歌曲数量?

任何帮助将不胜感激。

我的代码如下。

 DirectoryInfo di = new DirectoryInfo(@"C:/Sounds/Custom Sounds/");
        FileInfo[] filepaths = MultipleFileFilter(di, "*.mp3,*.wma,*.wav");
        for (int i = 0; i < filepaths.Length; i++)
        {
            if (Path.GetFileNameWithoutExtension(Convert.ToString(filepaths[i]))== deleteItem)
            {

                System.IO.FileInfo fi = new System.IO.FileInfo(@"C:/Sounds/Custom Sounds/" + Convert.ToString(filepaths[i]));
                fi.Delete();
            }
        }

// Load the sounds
        DirectoryInfo dii = new DirectoryInfo(@"C:/Sounds/Custom Sounds/");
        FileInfo[] filess = MultipleFileFilter(dii, "*.mp3,*.wma,*.wav");
        for (int i = 0; i < filess.Length; i++)
        {
            RadioButton rb = new RadioButton();
            rb.Text = Path.GetFileNameWithoutExtension(filess[i].ToString());
            rb.Name = Path.GetFileName(filess[i].ToString());
            rb.CheckedChanged += new EventHandler(radioButton_Update);
            Point lc = new Point(15, yPos);
            rb.Location = lc;
            panelSound.Controls.Add(rb);
            lstbxCustomSounds.Items.Add(Path.GetFileNameWithoutExtension(rb.Text));

            yPos += 20;
            //add into the arraylist
            controlsCollections.Add(rb);
        }

也许这就是问题所在?因为只有当前正在播放的声音被删除才会导致这个问题?

private void wPlayer_PlayStateChange(int newstate)
    {

        if (newstate == 1 )//Media finished
        {
            btnPlay.Text = "Play";
            wplayer.close();
            btnDelete.Enabled = true;
            wplayer.URL = "";
        }
        else if (newstate == 3)
        {
            btnDelete.Enabled = false;
        }

My application does this, play the selected sound using wmplib and upon statechangeevent = 1 (media player stopped), I will close the player using the close() command and setting the URL to "";

Now every time, I tried to delete a sound that is currently playing, in actual fact, it will be deleted, because when I looked at the directory, the file has been deleted. Every time after I delete a file, I will reload the controls which will list be the media file within the directory. However, it will still find the sound that is already been deleted in the directory.

I have tried this way, debugging slowly, and I found out that the File info delete() command takes a longer time to delete, being the fact that closing the media player takes a longer time for it to delete, that's why whenever I try reloading the collections after deleting a currently playing song, the deleted song is still there.
When I tried to delete when the media player is not playing the song, it deletes fast and the reloading collections did show the result of the song being gone.

The question is, how can I delete the file that is currently being played, ensuring that the reloading collections will show the correct number of song in the directory?

Any help would be greatly appreciated.

My code is as below.

 DirectoryInfo di = new DirectoryInfo(@"C:/Sounds/Custom Sounds/");
        FileInfo[] filepaths = MultipleFileFilter(di, "*.mp3,*.wma,*.wav");
        for (int i = 0; i < filepaths.Length; i++)
        {
            if (Path.GetFileNameWithoutExtension(Convert.ToString(filepaths[i]))== deleteItem)
            {

                System.IO.FileInfo fi = new System.IO.FileInfo(@"C:/Sounds/Custom Sounds/" + Convert.ToString(filepaths[i]));
                fi.Delete();
            }
        }

// Load the sounds
        DirectoryInfo dii = new DirectoryInfo(@"C:/Sounds/Custom Sounds/");
        FileInfo[] filess = MultipleFileFilter(dii, "*.mp3,*.wma,*.wav");
        for (int i = 0; i < filess.Length; i++)
        {
            RadioButton rb = new RadioButton();
            rb.Text = Path.GetFileNameWithoutExtension(filess[i].ToString());
            rb.Name = Path.GetFileName(filess[i].ToString());
            rb.CheckedChanged += new EventHandler(radioButton_Update);
            Point lc = new Point(15, yPos);
            rb.Location = lc;
            panelSound.Controls.Add(rb);
            lstbxCustomSounds.Items.Add(Path.GetFileNameWithoutExtension(rb.Text));

            yPos += 20;
            //add into the arraylist
            controlsCollections.Add(rb);
        }

Maybe this is the problem? Because only the currently playing sound that is being deleted will cause this problem?

private void wPlayer_PlayStateChange(int newstate)
    {

        if (newstate == 1 )//Media finished
        {
            btnPlay.Text = "Play";
            wplayer.close();
            btnDelete.Enabled = true;
            wplayer.URL = "";
        }
        else if (newstate == 3)
        {
            btnDelete.Enabled = false;
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念三年u 2024-08-29 15:56:40

似乎这个问题的一个简单答案是不允许删除当前正在播放的内容......或者确保删除过程在重新加载集合之前完成。

编辑根据上面的代码,当您实例化要删除的文件信息时,您将 Convert.ToString(filepath[i]) 附加到文件路径新的 fileinfo 对象。相反,我会删除之前的所有内容,只使用 filepath[i].FullName

因此,我只需将要删除的对象实例化为 new FileInfo(filepath[i].FullName);

It seems to be that an easy answer to this problem would be to not allow the deletion of something that is currently being played...either that or ensure that the delete process is complete before reloading the collection.

EDIT Based off your code above, when you're instantiating the fileinfo that is to be deleted, you're appending Convert.ToString(filepath[i]) to the filepath of the new fileinfo object. Instead, I'd get rid of everything before that and just use filepath[i].FullName.

So I'd just instantiate the object to be deleted as new FileInfo(filepath[i].FullName);.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文