私の画像のURLは次のようなものです:
photo\myFolder\image.jpg
変更したいので、次のようにします。
photo\myFolder\image-resize.jpg
それを行う短い方法はありますか?
メソッドを使用できますPath.GetFileNameWithoutExtension
。
指定されたパス文字列のファイル名を拡張子なしで返します。
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
ここにデモがあります。
これは私がファイルの名前変更に使用するものです
public static string AppendToFileName(string source, string appendValue)
{
return $"{Path.Combine(Path.GetDirectoryName(source), Path.GetFileNameWithoutExtension(source))}{appendValue}{Path.GetExtension(source)}";
}
または File.Move メソッド:
System.IO.File.Move(@"photo\myFolder\image.jpg", @"photo\myFolder\image-resize.jpg");
ところで: \ は相対パスであり、/ Web パスであることを覚えておいてください。
これを試して
File.Copy(Server.MapPath("~/") +"photo/myFolder/image.jpg",Server.MapPath("~/") +"photo/myFolder/image-resize.jpg",true);
File.Delete(Server.MapPath("~/") + "photo/myFolder/image.jpg");