Click here to Skip to main content
14,450,535 members
Rate this:
Please Sign up or sign in to vote.
See more:
i save a file to a folder using:

private void btnsave_Click(object sender, EventArgs e)
    string path = @"H:\le sensored\le sensored\images";
    string name = "Osim";
    pictureBox6.Image.Save(path + @"\" + name + ".jpg", ImageFormat.Jpeg);

but when i try to save another picture it overwrites the 1st one,please can i save other image in that same file path?
Updated 22-Apr-15 3:08am
Richard MacCutchan 22-Apr-15 8:11am
No, use a different filename.
Rate this:
Please Sign up or sign in to vote.

Solution 1

When you try to save a file with a file name of an existing file
it will be overwritten.

You must use a different file name.

Maybe you should use a counter that holds the number
of saved pictures and increase it after a file has been written.

The file name could e.g. be a combination of "Osim" and the current value of the counter.

Then your files would be called e.g. Osim_1.jpg, Osim_2.jpg, Osim_3.jpg etc.
Rate this:
Please Sign up or sign in to vote.

Solution 2

To do that, you will have to do one of two things:
1) Rename the existing file to something else first - File.Move[^] will do that for you, but you may have to check if that file exists as well.
2) Use a different name.

In either case, check if the file exists with File.Exists[^] first.

To use a different name, I'd suggest that you append a version number just before the file extension - and if you use an integer value to can check if it exists, and increment it if it does, then recheck.
So you would end up with

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100