Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8737861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:37:01+00:00 2026-06-13T10:37:01+00:00

I am beginner in c#. In my project, the user selects a Image file

  • 0

I am beginner in c#. In my project, the user selects a Image file through OpenFileDialog box. When he/she selects the image file I am running the back code something like this:

File.Copy(SourceFilePath, DestinationFilePath);

The problem with the above code is that it is throwing error whenever the user is trying to add a existing image file. To avoid this error, I changed my code to the below one:

if (File.Exists(DestinationFilePath))
{
     intCount++;
     File.Copy(SourceFilePath,TemporaryFilePath);
     File.Copy(TemporaryFilePath, DestinationFilePath + intCount.ToString());
     File.Delete(TemporaryFilePath);                                
}
else
{
     File.Copy(SourceFilePath, DestinationFilePath);
}

The problem in the above code is that it is adding the intCount value at the very end of the image file like image.gif1 which is changing the file extension. How to add a counter to image file paths?

And I think the approach I am using here to check the existing file is not correct way of doing.

Update: Answer :-

        int intCount = 1;
        while (File.Exists(Application.StartupPath + DirectoryPath + strPath))
        {
            strPath = Path.GetFileNameWithoutExtension(strPath) + intLarge.ToString() + Path.GetExtension(strPath);
            intCount++;
        }
        intCount = 1;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:37:02+00:00Added an answer on June 13, 2026 at 10:37 am

    use

    Path.GetFileNameWithoutExtension(string destinationfilename)
    

    add int to it and the add the extension to it which you can get by

    Path.GetExtension(string destinationfilename)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am beginner in c#. In my project, I populated a xml file inside
I am still a python beginner. As a practice project I want to code
I am a beginner in Java and am working on a Mastermind project for
I'm a Beginner, trying to build a django project that runs on Google App
I am a beginner in ASP.Net. I am starting a new project using ASP.Net
I maybe need to do a project in Delphi and are a beginner in
I am using NHibernate for my project. i am quite a beginner at working
Beginner in Android development. My code crashes. I have made a simple Java method
I am a beginner coder, i am building a project using C# Asp.Net in
I currently have a small (I'm a complete beginner) project in VTK where I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.