Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hello...i want to change the extension of images in the disk to the ico extension what's the wrong of my code
string ic = "ico";
foreach (var fil in Directory.GetFiles(Server.MapPath("~/minimalcolors/"))) {
string ext = fil.Substring(fil.IndexOf("."));
ext.Replace(ext, ic);
Posted 28-Feb-13 2:50am
Comments
ThePhantomUpvoter at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You should use Path.ChangeExtension method. MSDN Link[^]
var result = Path.ChangeExtension(yourfile, ".jpg");
If you want to physically change the extension, you could use File.Move method:
File.Move(yourfile, Path.ChangeExtension(yourfile, ".jpg"));
Good luck,
OI
  Permalink  
v2
Comments
Amro Mustafa at 28-Feb-13 9:03am
   
thank you....i will try
ridoy at 28-Feb-13 12:57pm
   
+5
Sergey Alexandrovich Kryukov at 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 at 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 at 11-Mar-13 10:10am
   
Thank you, Orcun.
—SA
Orcun Iyigun at 11-Mar-13 10:14am
   
Btw 5ed :) as usual.

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

  Print Answers RSS
0 OriginalGriff 6,656
1 Sergey Alexandrovich Kryukov 6,490
2 CPallini 5,310
3 George Jonsson 3,584
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web04 | 2.8.140921.1 | Last Updated 10 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100