how to save a file in VB.NET? - 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)

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 use saveFileDialog with XmlTextWriter

i am new with VB.net
and i manage to write input to xml file using
Dim writer As New XmlTextWriter("xmlInputFile.xml", System.Text.Encoding.UTF8)
now i want user to write they own name and path location of the xml file .
reading couple of example of SaveFileDialog and i come out with this code
Dim path As String = IO.Path.GetFullPath(SaveFileDialog1.FileName)
Dim writer As New XmlTextWriter(ToString(path), System.Text.Encoding.UTF8)
which give an error
thanks
btw the button code below
Private Sub Button3_Click_3(sender As Object, e As EventArgs) Handles btnSave.Click
Dim SaveFileDialog1 As SaveFileDialog = New System.Windows.Forms.SaveFileDialog
SaveFileDialog1.DefaultExt = ".xml"
SaveFileDialog1.Filter = "XML files(.xml)|*.xml|all Files(*.*)|*.*"
SaveFileDialog1.InitialDirectory = "desktop"
If SaveFileDialog1.ShowDialog = System.Windows.Forms.DialogResult.OK Then
Save_Data()
End If
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);

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 do I use savefiledialog in vb.net

I have a program called TextEditPro and I just started it, I'm running into a problem.
When I had the code for clicking Save As... I don't know how to use the savefiledialog so when you click Save As it will pop up!
Any help?
Learn to use MSDN - the documentation for SaveFileDialog has an example
Private Sub button1_Click(sender As Object, e As System.EventArgs)
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
' Code to write the stream goes here.
myStream.Close()
End If
End If
End Sub