Click here to Skip to main content
15,885,914 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello...i want to change the extension of images in the disk to the ico extension what's the wrong of my code
C#
string ic = "ico";
foreach (var fil in Directory.GetFiles(Server.MapPath("~/minimalcolors/"))) {
string ext = fil.Substring(fil.IndexOf("."));
ext.Replace(ext, ic);
Posted
Comments
[no name] 28-Feb-13 8:53am    
You do know that Replace returns a new string right? All you are doing is throwing the result away.
Sergey Alexandrovich Kryukov 11-Mar-13 10:12am    
Of course, but the problem is different. Why would anyone change "extension" of some image to icon? Please see my answer...
—SA

It is absolutely useless to "change extensions" of anything. It has nothing to do with actual file format.
Actually, there is no such thing as "extension". By historical reasons, either last three characters at the end of the file name, or all characters after '.' at the end are called "extension". In obsolete FAT file system and MS-DOS, all file names were 8-character name and 3-character "extension". In newer systems, there are no such thing anymore, but a part of file name at the end (as I described above) is used to identify "file type". It does not mean much; it's just that the Windows Shell tried to look up the Windows registry to find the "default application" used to open the file. It cannot help to read the file if the "extension" does not match its actual content and format.

—SA
 
Share this answer
 
You should use Path.ChangeExtension method. MSDN Link[^]
C#
var result = Path.ChangeExtension(yourfile, ".jpg");

If you want to physically change the extension, you could use File.Move method:
C#
File.Move(yourfile, Path.ChangeExtension(yourfile, ".jpg"));

Good luck,
OI
 
Share this answer
 
v2
Comments
Amro Mustafa 28-Feb-13 9:03am    
thank you....i will try
ridoy 28-Feb-13 12:57pm    
+5
Sergey Alexandrovich Kryukov 10-Mar-13 20:05pm    
It cannot help to "convert" icon to JPEG or anything like that, totally useless. Just a wrong answer, sorry. Formally, it will change exception, yes, but can you see what OP is trying to do? This is just misleading, for this "purpose".
—SA
Orcun Iyigun 11-Mar-13 3:39am    
After seeing your answer I agree to it but in my answer my purpose is not to convert to JPEG as you mentioned. It is just an example, I know you understand that part. Heck, I don't know which extension he wants to convert. It would be totaly ridicilous to change the extension from mp3 to jpeg since they are not matching in format either. But you have seen the bigger picture. Kudos to you.
Sergey Alexandrovich Kryukov 11-Mar-13 10:10am    
Thank you, Orcun.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900