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
Switch to:
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.