IT-Factory
asked on
cameradialog.initialdirectory file.exist
I use File.Exists(cameraDialog.I nitialDire ctory & 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:
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
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
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.InitialDirect ory 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.
The problem is that at the time the test File.exist is executed the property cameradialog.InitialDirect
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?
ASKER
At the start I initiate it with only the filename (cameraDialog.DefaultFileN ame = 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.DefaultFileN ame = dir & currFileName)
I found this:
Environment.GetFolderPath( Environmen t.SpecialF older.MyPi ctures);
but it is C# but when I change this to vb syntax the MyPictures is not valid.
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.DefaultFileN
I found this:
Environment.GetFolderPath(
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
http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.checkfileexists.aspx
ASKER
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( Environmen t.SpecialF older.MyPi ctures)
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(
Can you describe the sequence of code? What camera and picture are you talking about?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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);
ASKER
ok back in the running...
ok I test the last 2 comments
ok I test the last 2 comments
ASKER
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.
Thanks for getting me in the right way.
Open in new window