zey zey - 5 months ago 36
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 ?


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.