zey zey - 1 month ago 6
ASP.NET (C#) Question

Change file name of image path in C#

If my image URL is likes ,

photo\myFolder\image.jpg


How I want to change is likes ,

photo\myFolder\image-resize.jpg


Is there any short way to do it ?

Answer

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.