[RESOLVED] [2005] get the filename only from the open file dialog
Hi guys,
I have 2 questions
1. I wanna save a picture but only the filename only not the whole path.
how do I do that using open file dialog.
2. Let say I did save that file, how do I copy that file from the origin path to
specific path
Thanks guys.
Re: [2005] get the filename only from the open file dialog
use openfiledialogue.filename property
Re: [2005] get the filename only from the open file dialog
but it stores the whole path. like "C:/blabla/hkbkabek/a.jpg"
I just want the a.jpg. thanks for helping anyway.
Re: [2005] get the filename only from the open file dialog
Re: [2005] get the filename only from the open file dialog
Actually, no
do
vb.net Code:
TempFile As New System.IO.FileInfo(ofd.FileName)
TempFile.Name (the name includes the extension)
Re: [2005] get the filename only from the open file dialog
another alternative would be:
Dim FileNameOnly As String = System.IO.Path.GetFileName(ofd.FileName)
Both Path.GetFileName and FileInfo work really well and is self documenting
Re: [2005] get the filename only from the open file dialog
GREAT!! I can't wait to try those codes. Anyway thank you for helping guys.
oh by the way is there anyone that could me for question # 2? I did some research last night, I tried it but it's not working. I will post my code later today because I am still at school.
Re: [2005] get the filename only from the open file dialog
System.IO.File.Copy
Look it up on MSDN for a code example
Re: [2005] get the filename only from the open file dialog
yayyy the fileNameOnly works. I have one quick question.
in my form load I use the following code to load the image
Code:
currentRow = Me.BongoComicsDataSet.Characters.Rows(index)
lblName.Text = currentRow("name")
Pict1.Image = Image.FromFile("Characters\" & CStr(currentRow("image")))
it works just fine BUT everytime I add new picture I want automatically reload again the first picture, So what I did is using the same code to load it but it says FileNotFound. is the path changing everytime I add new picture from different folder?
it seems the program didn't know where is
Code:
Pict1.Image = Image.FromFile("Characters\" &CStr(currentRow("image")))
because when I add picture I add a picture form "My documents" or "desktop"
Re: [2005] get the filename only from the open file dialog
The picture is created by you yes? and 'currentRow("image")' contains the path to the image?, and how are you creating a new picture?
Re: [2005] get the filename only from the open file dialog
This is how I add the new picture
Code:
fileNameOnly = System.IO.Path.GetFileName(OFD.FileName)
CharactersTableAdapter.Insert(txtName.Text, fileNameOnly, txtAKA.Text)
Me.Validate()
Me.CharactersTableAdapter.Update(Me.BongoComicsDataSet.Characters)
Me.CharactersTableAdapter.Fill(Me.BongoComicsDataSet.Characters)
Yes it contains the path BUT not the whole path. What I mean not the whole path is, it contains only "Characters/filename.jpg" not "C:\Documents and Settings\erika\My Documents\Visual Studio 2005\Projects\BongoComics\BongoComics\bin\Debug\Characters\filename.jpg"
it works fine if I put the whole code, but this codes won't work in someone else computer because the path might be different.
the same thing when I copy the picture from the sourcePath to destinationPath. It will work if I put the whole path.
this is my code to copy the file
Code:
My.Computer.FileSystem.CopyFile(filename, "C:\Documents and Settings\erika\My Documents\Visual Studio 2005\Projects\BongoComics\BongoComics\bin\Debug\Characters\" & fileNameOnly)
Re: [2005] get the filename only from the open file dialog
if you are trying to indicate the current directory the application is running in (or a subdirectory of that) you should use Application.StartupPath
When you run your app, the actual EXE right now is located at
C:\Documents and Settings\erika\My Documents\Visual Studio 2005\Projects\BongoComics\BongoComics\bin\Debug\
However that is obviously not where it will always exist.
So the variable Application.StartupPath will be equal to
C:\Documents and Settings\erika\My Documents\Visual Studio 2005\Projects\BongoComics\BongoComics\bin\Debug\
if your exe was located in
c:\program files\my company\my app\
then that would be the value of application.startuppath (minus the final \)
So what I am getting at is, try
Code:
My.Computer.FileSystem.CopyFile(filename, Application.StartupPath & "\characters\" & filenameonly)
Re: [2005] get the filename only from the open file dialog
AWESOME kleinma!!! thanks a lot. Thank you all guys. I really appreciate it.
Re: [2005] get the filename only from the open file dialog
Quote:
Originally Posted by
ayahnabunda
but it stores the whole path. like "C:/blabla/hkbkabek/a.jpg"
I just want the a.jpg. thanks for helping anyway.
use this code:
dim filnam as string = OpenFileDialog.SafeFileName
Re: [2005] get the filename only from the open file dialog
Quote:
Originally Posted by
zgjir
use this code:
dim filnam as string = OpenFileDialog.SafeFileName
The OFD didn't have that back in 2007, this is a super old thread & needn't be brought up again.