Issue with OpenFileDialog and InitialDirectory property - vb.net

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...

Related

Rename file extension of selected file from dialog box 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

VB.NET filecopy program Error : System.NullReferenceException in FolderBrowserDialog1

I am trying to make a program which copies files from one folder to another and I am also using a progress bar to see what files are copying. When I am running this program I am getting an error on folderbrowserdialog1. The error is System.NullReferenceException. When I am running the program I am able to open the application but when i select the button it gives me an error.
Public Class Form1
Dim my As System.Diagnostics.Process = System.Diagnostics.Process.GetCurrentProcess
Dim directoryTargetLocation As String 'Selected file path
Dim Destinydirectory As String 'Selected dest directory path
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim FolderBrowserDialog1 As Object = Nothing
FolderBrowserDialog1.Description = "Select directory" **// Gettig error in this Line**
With FolderBrowserDialog1
If .ShowDialog() = DialogResult.OK Then
directoryTargetLocation = .SelectedPath
TextBox1.Text = directoryTargetLocation.ToString
Button2.Enabled = True
End If
End With
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
FolderBrowserDialog2.Description = "Select destiny directory"
With FolderBrowserDialog2
If .ShowDialog() = DialogResult.OK Then
Destinydirectory = .SelectedPath
TextBox2.Text = Destinydirectory.ToString
TextBox2.Text = TextBox2.Text.Remove(TextBox2.Text.Length - 1) &
TextBox1.Text.Substring(TextBox1.Text.LastIndexOf("\"))
Button3.Enabled = True
End If
End With
End Sub
You declared FolderBrowserDialog1 as an object.
What you want to do is to declare it as a FolderBrowserDialog.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FolderBrowserDialog1 As New FolderBrowserDialog
FolderBrowserDialog1.Description = "Select directory"
With FolderBrowserDialog1
If .ShowDialog() = DialogResult.OK Then
directoryTargetLocation = .SelectedPath
TextBox1.Text = directoryTargetLocation.ToString
Button2.Enabled = True
End If
End With
End Sub
You have not created a FolderBrowserDialog1 object.
Edit
You were close!
What you need is:
Dim FolderBrowserDialog1 As New FolderBrowserDialog
This creates a new FolderBrowserDialog object.
What you have is:
Dim FolderBrowserDialog1 As Object = Nothing
Which creates a new generic object name FolderBrowserDialog1, and explicitly sets it to be "nothing"
This means it does not have any of the properties or methods you were referencing, so you get the error.

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

Save Data to text tile using SaveFileDialog?

I have already viewed the MSDN Example but I am still having problems.
I created a super-simple program to multiply two numbers, and display the output in the textbox. Now I need to be able to read that text box value and put the value in a text file, bringing up the save to file dialog when the "Save To File" button is clicked.
Private Sub MutiplyBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MutiplyBtn.Click
Dim FirstNum As Double = Num1.Text
Dim SecondNum As Double = Num2.Text
Dim Answer2 As Double = FirstNum * SecondNum
Answerbox.Text = Answer2
End Sub
Private Sub SaveResultToFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveResultToFile.Click
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = saveFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
System.IO.File.WriteAllText(Answerbox.Text)
myStream.Close()
End If
End If
End Sub
Currently, Visual Studio is giving me an error: Overload resolution failed because no accessible 'WriteAllText' accepts this number of arguments.
WriteAllText static method requires the name of the file where the data should be written to.
You could use directly the name selected in the saveFileDialog1
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
System.IO.File.WriteAllText(saveFiledialog1.FileName, Answerbox.Text)
End If
instead if you really want to use the stream opened by OpenFile() method your code should be
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Dim sw As StreamWriter = new StreamWriter(saveFileDialog1.OpenFile())
If (sw IsNot Nothing) Then
sw.WriteLine(Answerbox.Text)
sw.Close()
End If
End If
The code is an example, you need to add a bit of error handling
Hi I tried above method but I succeed in this way....
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK _
Then
My.Computer.FileSystem.WriteAllText _
(SaveFileDialog1.FileName, RichTextBox1.Text, True)
End If
End Sub

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