c# - 如何替换2个文件?

标签 c# .net winforms

我有这段代码

    private List<string> FolderOne(string Folder)
    {

        string filena;
        DirectoryInfo dir = new DirectoryInfo(Folder);
        FileInfo[] files = dir.GetFiles("*.mp3", SearchOption.AllDirectories);

        List<string> str = new List<string>();

        foreach (FileInfo file in files)
        {
            str.Add(file.FullName);
                      filena = file.FullName;
                            filena.Replace("*.mp3", "*.jpg");
                            if (filena.Length > 0)
                            {
                                pictureBox1.Image = new System.Drawing.Bitmap(filena.ToString()); //I receive a error "Parameter is not valid."
                            }
        }
        return str;

    }

我的目的是让图片框读取同一文件夹中的文件全名“.mp3”但以“.jpg”结尾,事实上我在一个文件夹中有 2 个文件,第一个是歌曲“firstsong.mp3” ”和第二张图片“firstsong.jpg”,它们之间的区别是最终扩展名,因此我尝试读取相同文件名但扩展名为“.*jpg”的图片框,但我收到错误“参数无效。 “在行代码“pictureBox1.Image = new System.Drawing.Bitmap(filena.ToString());”中。 我怎么能解决这个问题? 感谢您的关注

亲切的问候

最佳答案

您的代码还有一些其他问题。首先,您要存储所有 mp3 文件名,但只显示最后加载的图像。

就替换扩展而言,使用Path 的方法来做到这一点:

string musicFile = "mysong.mp3";
string imageFile = Path.ChangeExtension(musicFile, "jpg");

关于c# - 如何替换2个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/782888/

相关文章:

c# - ACE OLEDB "External table is not in the expected format"在 Excel 单元格中带有大文本

c# - 通过网络加密字符串,c#

c# - 事件不按顺序发生

c# - 启用基于文本框和单选按钮的按钮

c# - 启动画面在关闭前向上移动

c# - ASP.NET MVC 使用数据注释迭代模型属性

c# - LINQ查询错误中的匿名函数

c# - 在其阻塞版本中使用方法的异步实现

.net - elasticsearch或RavenDB在插入统计引擎/随机森林方面会更好吗?

c# - 如何使用 LINQ to Entities 包含另一个表中的一个特定行