BP Forums
getting a file name form a save dialoge - Printable Version

+- BP Forums (https://bpforums.info)
+-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55)
+--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56)
+---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8)
+----- Forum: Programming Help (https://bpforums.info/forumdisplay.php?fid=9)
+----- Thread: getting a file name form a save dialoge (/showthread.php?tid=590)



getting a file name form a save dialoge - zmanalpha - 08-02-2012

Hey, I was just wondering how to get the actual fine name(not including the path) of a file chosen from a save file dialog. If anyone knows, help would be much appreciated! thanks!


Re: getting a file name form a save dialoge - Moldraxian - 08-02-2012

Do you mean like finding the actual file of your program inside your computer files?


Re: getting a file name form a save dialoge - Vinwarez - 08-02-2012

I wrote a small application for saving a file. Here is the code:
[code2=vbnet]Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim SaveFile As New SaveFileDialog
SaveFile.Title = "Save file"
SaveFile.Filter = "Text files (*.txt)|*.txt"
SaveFile.InitialDirectory = "C:\"
SaveFile.RestoreDirectory = True
Dim d = SaveFile.ShowDialog()

If d = DialogResult.OK Then
Try
Dim DocumentTitle As String = System.IO.Path.GetFileName(SaveFile.FileName)
Me.Text = DocumentTitle & " - Software Name"

Dim W As New System.IO.StreamWriter(SaveFile.FileName)
W.Write(RichTextBox1.Text)
W.Close()
Catch ex As Exception
MessageBox.Show("You have encountered an error." & Environment.NewLine & Environment.NewLine & "ERROR: " & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
End Class[/code2]

Since you don't need that, here is the line that you need:
[code2=vbnet]Dim DocumentTitle As String = System.IO.Path.GetFileName(SaveFile.FileName)
Me.Text = DocumentTitle & " - Software Name"[/code2]


Re: getting a file name form a save dialoge - brandonio21 - 08-04-2012

You can also use
[code2=vbnet]System.IO.Path.GetFileNameWithoutExtension()[/code2]

In order to get the file name without the ".exe", etc!