Link to home
Start Free TrialLog in
Avatar of IT-Factory
IT-FactoryFlag for Belgium

asked on

cameradialog.initialdirectory file.exist

I use File.Exists(cameraDialog.InitialDirectory & currFileName) with currFilename the full name of the file.

Problem is that initialdirectory is empty and I need to test if file already exist, if yes, change the filename and test again.

How can I test if filename already exist?

This is my code used:

            currFileName = Me.txtBonNr.Text & "-" & LPAD(3, ivolgNr.ToString, "0") & ".jpg"
            Do While File.Exists(cameraDialog.InitialDirectory & currFileName)
                ivolgNr += 1
                currFileName = Me.txtBonNr.Text & "-" & LPAD(3, ivolgNr.ToString, "0") & ".jpg"
            Loop
            cameraDialog.DefaultFileName = currFileName
            cameraDialog.Title = "Foto: " & currFileName

Open in new window

Avatar of kaufmed
kaufmed
Flag of United States of America image

See if this works better:
currFileName = Path.Combine(cameraDialog.InitialDirectory, String.Format("{0}-{1:000}.jpg", Me.txtBonNr.Text, ivolgNr))

Do While File.Exists(cameraDialog.InitialDirectory & currFileName)
    ivolgNr += 1
    currFileName = Path.Combine(cameraDialog.InitialDirectory, String.Format("{0}-{1:000}.jpg", Me.txtBonNr.Text, ivolgNr))
Loop

cameraDialog.DefaultFileName = currFileName
cameraDialog.Title = "Foto: " & currFileName

Open in new window

Correction:
currFileName = Path.Combine(cameraDialog.InitialDirectory, String.Format("{0}-{1:000}.jpg", Me.txtBonNr.Text, ivolgNr))

Do While File.Exists(currFileName)
    ivolgNr += 1
    currFileName = Path.Combine(cameraDialog.InitialDirectory, String.Format("{0}-{1:000}.jpg", Me.txtBonNr.Text, ivolgNr))
Loop

cameraDialog.DefaultFileName = currFileName
cameraDialog.Title = "Foto: " & currFileName

Open in new window

Avatar of IT-Factory

ASKER

The problem is not the variable currfilename, that's ok.

The problem is that at the time the test File.exist is executed the property cameradialog.InitialDirectory is empty so results in \1234-001.jpg
But when cameradialog is ended and the file is saved the location is not \1234-001.jpg but is "\\My Documents\My Pictures\1234-001.jpg

So the propery cameradialog.Filename is set to 1234-001.jpg and after save it has the value \\My Documents\My Pictures\1234-001.jpg

I need the full path at the time the test is executed...(file.exist), the path that cameradialog uses for saving files.
So is correct directory name achieved at the start, or end of cameradialog?
At the start I initiate it with only the filename (cameraDialog.DefaultFileName = currFileName) at this moment because I can't get the current dir of cameradialog, it is always empty.

I need to know the dir where the file will be saved. So I can concatinate dir + filename to check if already exists. if exist change filename and test again. Or use the dir + filename to initiate it (cameraDialog.DefaultFileName = dir & currFileName)

I found this:
Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);

but it is C# but when I change this to vb syntax the MyPictures is not valid.
Why not do the check after showing the dialog then? Or better still, why not use the CheckFileExists property of the save file dialog which automatically shows the warning if the file already exists

http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.checkfileexists.aspx
because when I reach cameradialog return value (the moment the picture is taken), the file is already saved so its too late to act then.

It would also help to know in front what the location will be.

But this line won't work, he does not know mypictures

FilePath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
Can you describe the sequence of code? What camera and picture are you talking about?
ASKER CERTIFIED SOLUTION
Avatar of hjgode
hjgode
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here are two alternative options you don't need to check if the file exists.
// option 1
string defaultFileName = System.IO.Path.ChangeExtension(Guid.NewGuid().ToString("N"), ".jpg");
Console.WriteLine(defaultFileName);

// option 2
string defaultFileName2 = System.IO.Path.ChangeExtension(System.IO.Path.GetRandomFileName(), ".jpg");
Console.WriteLine(defaultFileName2);

Open in new window

ok back in the running...
ok I test the last 2 comments
You are right this is not changeable so I left it as it is and moved the file afterwards.

Thanks for getting me in the right way.