Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
3
Reply
How to rename a file when it is already exist in a folder ?
Ajit N
Jan 18 2017 6:42 AM
238
Reply
Hi, In desktop application i saving the images in folders but when i save the images second time it will overwrite. so i want to save the same images in folder with rename like Picture(1).jpg.
so please help me how can i do this.
i tried the following code but images with numbers are not incremented.
string
folderPath1 =
"E:\\CoachingClassImages\\HeaderInfoImages\\Logo\\"
;
if
(!Directory.Exists(folderPath1))
{
Directory.CreateDirectory(folderPath1);
}
//Save the file in folder
int
count = 1;
string
file =
"E:\\CoachingClassImages\\HeaderInfoImages\\Logo\\"
+ logo_picname;
string
filename = Path.GetFileNameWithoutExtension(logo_location);
string
extention = Path.GetExtension(filename);
string
fileLocation1 = file;
if
(File.Exists(file))
{
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
string
tempFileName =
string
.Format(
"{0}({1}){2}"
, filename, count++, extention);
File.Copy(fileLocation1, Path.Combine(
"E:\\CoachingClassImages\\HeaderInfoImages\\Logo\\"
, Path.GetFileName(tempFileName)),
true
);
}
else
{
File.Copy(fileLocation1, Path.Combine(
"E:\\CoachingClassImages\\HeaderInfoImages\\Logo\\"
, Path.GetFileName(filename)),
true
);
}
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
3
)
Next Recommended Forum
Resizing a PNG in a UWP project without losing transparency?
How to hide ajax call