Add a existing file to folder
<p>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:</p>

<pre><code>File.Copy(SourceFilePath, DestinationFilePath);
</code></pre>

<p>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:</p>

<pre><code>if (File.Exists(DestinationFilePath))
{
     intCount++;
     File.Copy(SourceFilePath,TemporaryFilePath);
     File.Copy(TemporaryFilePath, DestinationFilePath + intCount.ToString());
     File.Delete(TemporaryFilePath);                                
}
else
{
     File.Copy(SourceFilePath, DestinationFilePath);
}
</code></pre>

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

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

<p>Update: <strong>Answer</strong> :-</p>

<pre><code>        int intCount = 1;
        while (File.Exists(Application.StartupPath + DirectoryPath + strPath))
        {
            strPath = Path.GetFileNameWithoutExtension(strPath) + intLarge.ToString() + Path.GetExtension(strPath);
            intCount++;
        }
        intCount = 1;
</code></pre>
