How can I check if a file already exists?
<pre><code>public void SaveFormPicutreBoxToBitMapIncludingDrawings()
        {
            using (Bitmap b = new Bitmap(pictureBox1.Width, pictureBox1.Height))
            {
                pictureBox1.DrawToBitmap(b, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height));
                string fn = @"d:\PictureBoxToBitmap\" + PbToBitmap.ToString("D6") + ".bmp";
                if (File.Exists(fn))
                {
                }
                else
                {
                    b.Save(@"d:\PictureBoxToBitmap\" + PbToBitmap.ToString("D6") + ".bmp"); // to fix/repair give gdi error exception.
                    PbToBitmap++;
                }
            } 
        }
</code></pre>

<p>If i move the trackBar to the right it will save the first file 000000.bmp then raise it by one next time it will save file 000001.bmp</p>

<p>But if i move back to the left once now the variable fn is 000002.bmp wich dosent exist and it will save the previous image wich is realy 000001.bmp.</p>

<p>What it suppose to do when i move ot the left back it should be 000001.bmp see that it exist and do nothing.</p>

<p>If will not make this checking it will just keep saving files all the time if i move the trackBar to the right or to the left so after few times i will have more then 90 files wich are almost all the same.</p>

<h2>How can i solve it ?</h2>

<p>The variable PbtoBitmap is type of int in the top of Form1 i just started it with 0.
PbToBitmap = 0;</p>

<p>The trackBar im talking about is trackBar1 where in the scroll event im calling this SaveFormPicutreBoxToBitMapIncludingDrawings function .</p>

<p>This is the trackBar1 scroll event:</p>

<pre><code>private void trackBar1_Scroll(object sender, EventArgs e)
        {

            currentFrameIndex = trackBar1.Value;
            textBox1.Text = "Frame Number : " + trackBar1.Value;
            wireObject1.woc.Set(wireObjectAnimation1.GetFrame(currentFrameIndex)); 

            trackBar1.Minimum = 0;
            trackBar1.Maximum = fi.Length - 1;
            setpicture(trackBar1.Value);
            pictureBox1.Refresh();

            button1.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
            button4.Enabled = false;
            button8.Enabled = false;
            SaveFormPicutreBoxToBitMapIncludingDrawings();
            return;
        }
</code></pre>
