• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 447
  • Last Modified:

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

0
IT-Factory
Asked:
IT-Factory
  • 5
  • 3
  • 2
  • +2
1 Solution
 
käµfm³d 👽Commented:
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

0
 
käµfm³d 👽Commented:
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

0
 
IT-FactoryAuthor Commented:
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.
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
käµfm³d 👽Commented:
So is correct directory name achieved at the start, or end of cameradialog?
0
 
IT-FactoryAuthor Commented:
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.
0
 
CodeCruiserCommented:
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
0
 
IT-FactoryAuthor Commented:
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)
0
 
CodeCruiserCommented:
Can you describe the sequence of code? What camera and picture are you talking about?
0
 
hjgodeCommented:
Hello

sorry for jumping in, but I believe I understand your need.

The CameraCaptureDialog is a separate app and you can not enforce the user to use a special dir or name of a captured still image. You can specify an initial dir and a default file name, but the user may change these. When the dialog ends you get a DialogResult and if this is OK you can get the file name (and dir) the user has chosen to save the image.
Now you can move/rename the image file to your scheme, but this is after the cameracapturedialog has been closed. You can then inform the user and show the dir and file name you have enforced.

I don't see another way to enforce a dir or filename convention. The CameraDialog was not designed for this.

regards

Josef
0
 
MedievalWarriorCommented:
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

0
 
IT-FactoryAuthor Commented:
ok back in the running...
ok I test the last 2 comments
0
 
IT-FactoryAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now