1 Replies - 9289 Views - Last Post: 07 November 2009 - 05:25 AM Rate Topic: -----

#1 EndLessMind  Icon User is offline

  • Android Expert
  • member icon

Reputation: 270
  • View blog
  • Posts: 1,246
  • Joined: 13-March 09

read filenames from folder and show in combobox

Posted 07 November 2009 - 05:14 AM

Hi. I have a code that copies a picture to a folder and then check that files name. but when im going to display the filenames in a combobox i get the code path. like "C:\images\hello.jpg"
but i just want the "hello.jpg"

How do i do that?

Here is the code im using:

		Dim SAVE_PATH As String = Application.StartupPath & "\bg"
		Dim FileDialog As New OpenFileDialog
		Dim fso = My.Computer.FileSystem

		With FileDialog

			If Not fso.DirectoryExists(SAVE_PATH) Then
				Try
					fso.CreateDirectory(SAVE_PATH)
				Catch ex As Exception
					MessageBox.Show("Unable to create folder '" & SAVE_PATH.ToLower & _
									"'.  Images will be saved in '" & Application.StartupPath.ToLower & _
									"'.", Text, MessageBoxButtons.OK, MessageBoxIcon.Warning)
					SAVE_PATH = Application.StartupPath
				End Try
			End If

			.InitialDirectory = SAVE_PATH
			.Filter = "All Graphic Files|*.bmp;*.gif;*.jpg;*.jpeg;*.png;|" & _
			"Graphic Interchange Format (*.gif)|*.gif|" & _
			"Portable Network Graphics (*.png)|*.png|" & _
			"JPEG File Interchange Format (*.jpg;*.jpeg)|*.jpg;*.jpeg|" & _
			"Windows Bitmap (*.bmp)|*.bmp"
			.FilterIndex = 1
			.FileName = ""
			'Label13.Text = Application.StartupPath & "\Saved\" & fso.GetName(.FileName)
			If .ShowDialog = Windows.Forms.DialogResult.OK Then

				If .FileName.ToUpper = (SAVE_PATH & "\" & fso.GetName(.FileName)).ToUpper Then
					MessageBox.Show("The file '" & fso.GetName(.FileName).ToLower & "' cannot be copied onto itself.", _
									Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
					Exit Sub
				ElseIf fso.FileExists(SAVE_PATH & "\" & fso.GetName(.FileName)) Then
					Dim Response As DialogResult

					Response = MessageBox.Show("The file '" & fso.GetName(.FileName).ToLower & "' already exist in the destination folder '" & _
											   SAVE_PATH & "'" & vbCrLf & vbCrLf & "Do you want to overwite it?", Text, MessageBoxButtons.YesNo, _
											   MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)

					If Response = Windows.Forms.DialogResult.No Then Exit Sub
				End If

				Try
					fso.CopyFile(.FileName, SAVE_PATH & "\" & fso.GetName(.FileName), True)
				Catch ex As Exception
					MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
				End Try
			End If
		End With
		'Dim SAVE_image As String = Application.StartupPath & "\bg" & "\" & fso.GetName(.FileName)
		'Dim myImage As New Bitmap(SAVE_image)
		'PictureBox2.Image = myImage
		'Label13.Text = SAVE_image

		Dim sf As String
		For Each sf In Directory.GetFiles(SAVE_PATH & "\")
			ComboBox3.Items.Add(sf)
		Next



Thanks in advance.

This post has been edited by EndLessMind: 07 November 2009 - 05:15 AM


Is This A Good Question/Topic? 0
  • +

Replies To: read filenames from folder and show in combobox

#2 nmgod  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 48
  • View blog
  • Posts: 233
  • Joined: 26-March 08

Re: read filenames from folder and show in combobox

Posted 07 November 2009 - 05:25 AM

you want to show the name of the file you saved, or all the files in the save directory?

there is an easy way to show the filename alone

take your last bit of code

		Dim sf As String
		For Each sf In Directory.GetFiles(SAVE_PATH & "\")
			ComboBox3.Items.Add(sf)
		Next


change that to

		Dim sf As String
		For Each sf In Directory.GetFiles(SAVE_PATH & "\")
			ComboBox3.Items.Add(sf.Substring(sf.LastIndexOf("\") + 1, sf.Length - sf.LastIndexOf("\") - 1))
		Next


and for a file say
c:\dir\file.jpg
you will get
file.jpg
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1