Rename file extension of selected file from dialog box vb.net - vb.net

I am trying to rename the file extension of a file I selected from the dialog box. Right now, it is not changing format to ".txt". Where am I wrong?
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'Sym File Open
Dim sym As OpenFileDialog = New OpenFileDialog()
''Defines the critieria of a Trace file type.
sym.Title = "Open File Dialog"
sym.InitialDirectory = "C:\My Computer\Documents"
sym.Filter = "Symbol files (*.sym*)|*.sym*"
sym.FilterIndex = 1
sym.RestoreDirectory = True
'Assigning path to textbox
If sym.ShowDialog() = DialogResult.OK Then
SymTextSelection.Text = sym.FileName
'Converting sym file into a text file
sym.FileName = sym.FileName.Replace(".sym", ".txt")
End If
End Sub

This worked for me
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'Sym File Open
Dim sym As OpenFileDialog = New OpenFileDialog()
''Defines the critieria of a Trace file type.
sym.Title = "Open File Dialog"
sym.InitialDirectory = "C:\My Computer\Documents"
sym.Filter = "Symbol files (*.sym*)|*.sym*"
sym.FilterIndex = 1
sym.RestoreDirectory = True
'Assigning path to textbox
If sym.ShowDialog() = DialogResult.OK Then
SymTextSelection.Text = sym.FileName
'Converting sym file into a text file
File.Move(sym.FileName, Path.ChangeExtension(sym.FileName, ".xlsm"))
End If
End Sub

Related

Issue with OpenFileDialog and InitialDirectory property

I have two Buttons and two OpenFileDialogs and I am facing an issue with InitialDirectory property. When I choose a file for my first OpenFileDialog and then click to choose a file for my second OpenFileDialog, I get the same InitialDirectory and not the ones I have set!!!
Here is an example of my code...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FileDialog As New OpenFileDialog
Dim Path As String = Nothing
If Label1.Text IsNot Nothing And My.Computer.FileSystem.FileExists(Label1.Text) Then
Path = Label1.Text
Else
Path = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
End If
FileDialog.Title = "Open File Dialog"
FileDialog.InitialDirectory = Path
FileDialog.Filter = "Executable (*.exe)|*.exe"
FileDialog.RestoreDirectory = True
If FileDialog.ShowDialog() = DialogResult.OK Then
Label1.Text = FileDialog.FileName
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim FileDialog As New OpenFileDialog
Dim Path As String = Nothing
If Label2.Text IsNot Nothing And My.Computer.FileSystem.FileExists(Label2.Text) Then
Path = Label2.Text
Else
Path = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}"
End If
FileDialog.Title = "Open File Dialog"
FileDialog.InitialDirectory = Path
FileDialog.Filter = "Executable (*.exe)|*.exe"
FileDialog.RestoreDirectory = True
If FileDialog.ShowDialog() = DialogResult.OK Then
Label2.Text = FileDialog.FileName
End If
End Sub
Well I found what was wrong...
I just had to set the Path variable like this Path = IO.Path.GetDirectoryName(Label1.Text) and not like this Path = Label1.Text. Because the first one gets file's directory path (which is required) and the second one gets file's path. And I was using the second one...

how to save a file in VB.NET?

i'm trying to use the savefiledialog tool, but it don't create the file to the selected destination...
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "txt files (*.txt)|*.txt"
saveFileDialog1.Title = "Save a Text File"
If saveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK & saveFileDialog1.FileName.Length > 0 Then
RichTextBox2.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText)
End If
End Sub
The binary logical operator AND in VB.NET is expressed with the keyword AND not using & (string concatenating operator)
If saveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK And
saveFileDialog1.FileName.Length > 0 Then
....
If you set Option Strict On this problem will be signaled at compile time
(By the way, there is no need to test for the filename length. The dialog doesn't close if you don't supply a filename)

File is not shown in OpenFileDialog

I'm trying to do something like a text editor in VB 2012, so I have a MenuStrip with an "OpenFile" option. When it's clicked it fires an OpenFileDialog control and shows me just the files with the extension I want. The problem is that if I close the file with another option in the menu strip, when I want to open the same file I opened the first time using the same OpenFile option the OpenFileDialog is not showing me the file.
Do you know why is that?
Here is how I open the file:
Private Sub OpenFileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles OpenFileToolStripMenuItem.Click
'Open File
If OpenFile.ShowDialog() = Windows.Forms.DialogResult.OK Then
If Not (OpenFile.FileName = "") And OpenFile.CheckFileExists Then
NewFile = New StreamReader(OpenFile.FileName)
If NewFile IsNot Nothing Then
Me.TextBox_Main.Text = NewFile.ReadToEnd
Me.TabPage1.Text = OpenFile.FileName.Substring(OpenFile.FileName.LastIndexOf("\") + 1)
End If
End If
End If
End Sub
And here is how I close the file:
Private Sub CloseFileToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseFileToolStripMenuItem.Click
'Close Files
If NewFile IsNot Nothing Then
NewFile.Close()
NewFile = Nothing
End If
Me.TextBox_Main.Text = ""
Me.TabPage1.Text = "New Tab"
End Sub

Open only files with *.bin

It is possible to add open only files with *.bin extension in openfile dialog ?
Here is my code. Maybe somebody can fix it.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OFD As New OpenFileDialog
Dim fullFile() As Byte
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
fullFile = File.ReadAllBytes(OFD.FileName)
TextBox1.AppendText(fullFile(&H1E).ToString("X2") & " ")
TextBox1.AppendText(fullFile(&H1F).ToString("X2"))
End If
If file have another extension msg,box : Wrong file
You need to use the Filter property: MSDN
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim OFD As New OpenFileDialog
OFD.Filter = "BIN Files (*.bin)|*.bin"
Dim fullFile() As Byte
If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
fullFile = File.ReadAllBytes(OFD.FileName)
TextBox1.AppendText(fullFile(&H1E).ToString("X2") & " ")
TextBox1.AppendText(fullFile(&H1F).ToString("X2"))
End If
End Sub
The pipe | character is used in the filter string to separate it into chunks: the first is what the user sees in the dropdown, and the second is the actual filter that is run on the files. You can have multiple filters available, too. Here's another example of a filter string: Text files (*.txt)|*.txt|All files (*.*)|*.*
You need to use javax.swing.JFileChooser.
Use this:
FileNameExtensionFilter filter = new FileNameExtensionFilter("Binary Files", "bin");
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(filter);

How to specify path using open file dialog in vb.net?

In the first start of my application I need to specify a path to save some files to it. But in the open file dialogue it seems like that I have to select a file to open. How can I just specify a folder without oppening a file
like C:\config\
Here is my code
If apppath = "" Then
Dim fd As OpenFileDialog = New OpenFileDialog()
fd.Title = "Select Application Configeration Files Path"
fd.InitialDirectory = "C:\"
fd.Filter = "All files (*.*)|*.*|All files (*.*)|*.*"
fd.FilterIndex = 2
fd.RestoreDirectory = True
If fd.ShowDialog() = DialogResult.OK Then
apppath = fd.FileName
End If
My.Computer.FileSystem.WriteAllText(apppath & "apppath.txt", apppath, False)
End If
I need to select a file in order for it to work, but I just want to select a folder. So what's the solution?
You want to use the FolderBrowserDialog class instead of the OpenFileDialog class. You can find more information about it here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.folderbrowserdialog(v=vs.110).aspx
For instance, you could do this:
If apppath = "" Then
Dim dialog As New FolderBrowserDialog()
dialog.RootFolder = Environment.SpecialFolder.Desktop
dialog.SelectedPath = "C:\"
dialog.Description = "Select Application Configeration Files Path"
If dialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
apppath = dialog.SelectedPath
End If
My.Computer.FileSystem.WriteAllText(apppath & "apppath.txt", apppath, False)
End If
If I understand correctly, you want to let the user choose a folder. If that is the case, then you want to use FolderBrowserDialog instead of OpenFileDialog.
Dim filedialog As New OpenFileDialog
filedialog.IntialDirectory = Application.StartupPath
filedialog.ShowDialog()
Or you can simply just make it less lines and very simple.
link: http://i.imgur.com/bMq0HNz.png
Start your dialog with a click:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FolderBrowserDialog1.ShowDialog()
End Sub
Add if you want to show the actual path that you choose from your dialog
Private Sub FolderBrowserDialog1_Disposed(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = FolderBrowserDialog1.SelectedPath.ToString
End Sub
Use To:
Dim openFD As New OpenFileDialog()
Dim Directory As string = openFD.FileName
Try this
Private Sub BtnOpen_Click(sender As Object, e As EventArgs) Handles BtnOpen.Click
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "x_pathfileforsend"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|*.zip|*.rar|*.ico|*.exe|*.png|*.bmp|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 5
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = DialogResult.OK Then
txtpath.Text = openFileDialog1.FileName
End If
openFileDialog1.Dispose()
End Sub