tags:

views:

100

answers:

3

Hi, i have this code snippet

    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;

    }

My purpose was to make read the picture box the file.fullname ".mp3" in the same folder but end with ".jpg",infact i have 2 file in a folder the first one is a song "firstsong.mp3" and the second one a picture "firstsong.jpg" the difference between them is the final extension so i try to make read to picturebox the same filename but with extension ".*jpg" and i receive an error "Parameter is not valid." in the line code "pictureBox1.Image = new System.Drawing.Bitmap(filena.ToString());". How i can work out that? Thanks for your attention

Nice Regards

+3  A: 

Switch to:

filena = filena.Replace(".mp3", ".jpg");
if (filena.Length > 0)
{
    pictureBox1.Image = new System.Drawing.Bitmap(filena); 
}

The main problem is with filena.Replace("*.mp3", "*.jpg");

There are two issues in that line.

First, you're searching on "*.mp3" instead of just ".mp3". The individual filenames do not have the * character, and string.Replace doesn't use regular expressions, just string matching.

Second, strings in .NET are immutable. They cannot be changed once they're created. This means that you can't replace the value of a string in place - you always return a new string. So string.Replace(...) will return a new string.

Reed Copsey
-1. I think recommending the use string.Replace for a path is horrible practice. If the name has any other instances of ".mp3" (such as any directory path or filename), *all* those instances will be replaced, not just the extension as he is trying to do. Also, Replace is case-sensitive which might result is some wrong results.
Erich Mirabal
I actually agree - using the ChangeExtension method on Path is a better practice than the string replacing.I was more focused on explaining why his didn't work, less on giving him a different but probably better approach.
Reed Copsey
I undid the -1, but this info should probably be in the answer itself and not in the comments.
Erich Mirabal
+4  A: 

There are some other issues with your code. First off, you are storing all the mp3 file names, but only displaying the last image loaded.

As far as replacing the extension, use Path's method to do that:

string musicFile = "mysong.mp3";
string imageFile = Path.ChangeExtension(musicFile, "jpg");
Erich Mirabal
Also, there is no need to call ToString() on a string. It's a bit redundant.
Erich Mirabal
+1  A: 

I'd add to the previous suggestions by adding that you should check that the jpg exists by doing the following:

if (File.Exists(jpgFilePath)) {
     pictureBox1.Image = new System.Drawing.Bitmap(jpgFilePath);
}
Jay Riggs