zey zey - 1 year ago 110
ASP.NET (C#) Question

Change file name of image path in C#

If my image URL is likes ,


How I want to change is likes ,


Is there any short way to do it ?

Answer Source

You can use Path.GetFileNameWithoutExtension method.

Returns the file name of the specified path string without the extension.

string path = @"photo\myFolder\image.jpg";
string file = Path.GetFileNameWithoutExtension(path);
string NewPath = path.Replace(file, file + "-resize");
Console.WriteLine(NewPath); //photo\myFolder\image-resize.jpg

Here is a DEMO.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download