TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Ajit N
NA
352
71.2k
How to rename a file when it is already exist in a folder ?
Jan 18 2017 6:42 AM
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
);
}
Reply
Answers (
3
)
Resizing a PNG in a UWP project without losing transparency?
How to hide ajax call