28

My image URL is like this:

photo\myFolder\image.jpg

I want to change it so it looks like this:

photo\myFolder\image-resize.jpg

Is there any short way to do it?

3
  • Your question doesn't say a lot. Why would String.Replace not work for you? Commented Jun 19, 2013 at 6:33
  • @zey Can't you use Find and Replace? Commented Jun 19, 2013 at 6:34
  • 1
    no , file type and file name may be dynamic :) Commented Jun 19, 2013 at 6:35

7 Answers 7

30

This following code snippet changes the filename and leaves the path and the extenstion unchanged:

public static string ChangeFilename(string filepath, string newFilename)
{
    // filepath = @"photo\myFolder\image.jpg";
    // newFileName = @"image-resize";

    string dir = Path.GetDirectoryName(filepath);    // @"photo\myFolder"
    string ext = Path.GetExtension(filepath);        // @".jpg"

    return Path.Combine(dir, newFilename + ext); // @"photo\myFolder\image-resize.jpg"
}
Sign up to request clarification or add additional context in comments.

Comments

7

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.

1 Comment

If the path is images\myFolder\image.jpg your method will also change the imagespart of the path to image-resizes. I would get the directory path, the filename and the extension separately, change the filename and rebuild a path from all these elements.
5

I would use a method like this:

private static string GetFileNameAppendVariation(string fileName, string variation)
{
    string finalPath = Path.GetDirectoryName(fileName);

    string newfilename = String.Concat(Path.GetFileNameWithoutExtension(fileName), variation, Path.GetExtension(fileName));

    return Path.Combine(finalPath, newfilename);
}

In this way:

string result = GetFileNameAppendVariation(@"photo\myFolder\image.jpg", "-resize");

Result: photo\myFolder\image-resize.jpg

Comments

3

This is what i use for file renaming

public static string AppendToFileName(string source, string appendValue)
{
    return $"{Path.Combine(Path.GetDirectoryName(source), Path.GetFileNameWithoutExtension(source))}{appendValue}{Path.GetExtension(source)}";
}

Comments

2

Or the File.Move method:

System.IO.File.Move(@"photo\myFolder\image.jpg", @"photo\myFolder\image-resize.jpg");

BTW: \ is a relative Path and / a web Path, keep that in mind.

Comments

1

You can try this

 string  fileName = @"photo\myFolder\image.jpg";
 string newFileName = fileName.Substring(0, fileName.LastIndexOf('.')) + 
                     "-resize" + fileName.Substring(fileName.LastIndexOf('.'));

 File.Copy(fileName, newFileName);
 File.Delete(fileName);

1 Comment

File.Move would do your last 2 lines in one
0

try this

File.Copy(Server.MapPath("~/") +"photo/myFolder/image.jpg",Server.MapPath("~/") +"photo/myFolder/image-resize.jpg",true);
File.Delete(Server.MapPath("~/") + "photo/myFolder/image.jpg");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.