-3

I have files in a folder and I want to change their extensions they are like 123.pdf.kayityok ; they are already changed so what I want to do is make them .pdf again without .kayityok so after I choose folder and run the program it will make the file like 123.pdf. I havent used c# in a long time so please explain things in details. Thanks in advance.

RealSteel
  • 1,871
  • 3
  • 37
  • 74
Murat Ölter
  • 1
  • 1
  • 1

3 Answers3

1

Try this

There is: Path.ChangeExtension method.

var result = Path.ChangeExtension(myffile, ".jpg");

In the case if you also want to physically change the extension, you could use File.Move method:

File.Move(myffile, Path.ChangeExtension(myffile, ".jpg"));

Shrivallabh
  • 2,863
  • 2
  • 27
  • 47
0

Use File.Move to rename the file. If you supply the same directory in which the file already is, this will only rename the file then.

For example:

File.Move(@"C:\dir\123.pdf.kayityok", @"C:\dir\123.pdf");
Toni Petrina
  • 7,014
  • 1
  • 25
  • 34
  • i have many files in that folder and names are not stable so i need to read the names first and that is the main problem i am having right now, i already found renaming examples in other questions – Murat Ölter Mar 11 '13 at 07:15
  • Well, you can get list of all files via Directory.EnumerateFiles method. Have you tried that? – Toni Petrina Mar 11 '13 at 07:36
0

Check the Path class of the .NET framework (http://msdn.microsoft.com/en-us/library/3bdzys9w.aspx). The ChangeExtension() methos could help you (http://msdn.microsoft.com/en-us/library/system.io.path.changeextension.aspx)

Regards,

Manolis
  • 728
  • 8
  • 24