vb.net use saveFileDialog with XmlTextWriter - vb.net

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

Related

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)

Im cant get a doc from my documents to display

Imports System.IO
Public Class Form1
Private Sub MenuItem1_Click(sender As Object, e As EventArgs) Handles MenuItem1.Click
Me.txtFileContent.Text = Nothing
End Sub
Private Sub MenuItem2_Click(sender As Object, e As EventArgs) Handles MenuItem2.Click
Me.txtFileContent.Text = Nothing
End Sub
Private Sub MenuItem3_Click(sender As Object, e As EventArgs) Handles MenuItem3.Click
Me.savSaveFile.ShowDialog()
Dim strFileName As String = Me.savSaveFile.FileName
Dim fs As New FileStream(strFileName, FileMode.Create, FileAccess.Write)
Dim TextFile As New StreamWriter(fs)
TextFile.Write(Me.txtFileContent.Text)
TextFile.Close()
fs.Close()
End Sub
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MenuItem5.Click
Me.savSaveFile.ShowDialog()
Dim strFileName As String = Me.savSaveFile.FileName
Dim fs As New FileStream(strFileName, FileMode.Open, FileAccess.Read)
Dim TextFile As New StreamWriter(fs)
TextFile.Close()
fs.Close()
End Sub
End Class
What im trying to do is display a "open" document dialog box so the text inside the box can be displayed into the text box. I did the same with the "save" document dialog box and it worked fine just confused on the Open part. Thanks a million everyone.
There are two different kind of dialog box for selecting files.
One for save operations and one for opening files.
Your code seems to use the same one and from the name I think it is a SaveFileDialog.
To open a file you need a OpenFileDialog and use a StreamReader not a StreamWriter
Private Sub OpenToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MenuItem5.Click
' Create an OpenFileDialog here, for more precise property settings see the link above'
Dim openDlg = New OpenFileDialog()
' If user presses OK'
if openDlg.ShowDialog() = DialogResult.OK Then
' Still the filename selected could be empty, need to check'
Dim strFileName As String = openDlg.FileName
if strFileName.Trim().Length > 0 then
' Open the file using a reader, not a writer'
Using fs = New FileStream(strFileName, FileMode.Open, FileAccess.Read)
Using TextFile = New StreamReader(fs)
' Read everything (caution this should be used only for small files)'
Dim fileContent = TextFile.ReadToEnd
' Pass everything into a TextBox control for display'
Me.txtFileContent.Text = fileContent
End Using
End Using
End If
End If
End Sub

How do I get the file name only, assign it to a variable and use later?

I have the following code:
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()
Dim saveFileName As String = ""
saveFileDialog1.Filter = "txt files (*.txt)|*.txt"
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = saveFileDialog1.OpenFile()
saveFileDialog1.FileName = saveFileName
Using sw As StreamWriter = New StreamWriter(myStream)
' Add some text to the file.
sw.WriteLine(DateTime.Now + " - " + saveFileName) ' Date and File title header
sw.WriteLine("-------------------")
' Arbitrary objects can also be written to the file.
sw.Write("The date is: ")
sw.WriteLine(DateTime.Now)
sw.Close()
End Using
If (myStream IsNot Nothing) Then
' Code to write the stream goes here.
myStream.Close()
End If
End If
End Sub
The variable is saveFileName, however when I use the saveFileDialog1.FileName as it, it is empty or provides the full path to the file, how do I only get the name? (such as test.txt)
Use Path.GetFileName
saveFileName = Path.GetFileName(saveFileDialog1.FileName)
DIM fileName = IO.Path.GetFileName(SavefileDialog.FileName)

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

Trying to open a docx file from a bytestream - file corruption error

we consistently get a file corrupted error message when opening a docx file from a saved bytestream, evry other file type works just ok
Below is code from a sample form that replciate the issue
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Objective is to be able to copy a file to a bytestream then create a new document from that stream and then opne it.
'This replicates the behaviour of our primary application where it stores and retrieves the stream from a database
'With docx files we consistently experience some sort of corruption in the write of the original file
'When selecting .doc or other format files we do not encounter the same problem
'use selected file
Dim _o1 As String = TextBox1.Text
'get its bytestream
Dim fs As New FileStream(_o1, FileMode.Open, FileAccess.Read)
Dim byteStream(Convert.ToInt32(fs.Length)) As Byte
fs.Read(byteStream, 0, Convert.ToInt32(fs.Length))
'create a new file and use the bytestream to create it and save to disk
Dim _o As String = "C:\" & Now.Ticks & getNewFileName()
Dim fs1 As New FileStream(_o, FileMode.OpenOrCreate, FileAccess.Write)
Using bw As New BinaryWriter(fs1)
bw.Write(byteStream)
bw.Flush()
End Using
'open the new document
System.Diagnostics.Process.Start(_o)
Application.DoEvents()
End Sub
Private Function getNewFileName() As String
Dim fi As New FileInfo(TextBox1.Text)
Return Now.Ticks.ToString & fi.Name
End Function
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
OpenFileDialog1.InitialDirectory = "c:\"
OpenFileDialog1.FilterIndex = 2
OpenFileDialog1.RestoreDirectory = True
OpenFileDialog1.Filter = "docx files |*.docx"
If OpenFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
TextBox1.Text = OpenFileDialog1.FileName
End If
End Sub
Forgive me, but that is some messed up code.
Dim _o As String = "C:\" & Now.Ticks & getNewFileName()
will become...
Dim _o As String = "C:\" & Now.Ticks & Now.Ticks.ToString & fi.Name
Example result "C:\" "634015010433498951" "634015010433498951" "FileName.txt" is probably not what you are expecting unless you intend to subtract the two tick counts to determine how long it took to populate FileInfo.
Your FileStream corruption could be a encoding issue, off by one file length issue, or even a long filename in a deep path could a problem. Instead of using FileStream, this code should work fine:
Dim sourceFile As String = TextBox1.text
Dim fi As New System.IO.FileInfo(sourceFile)
Dim destFile = "C:\" & Now.Ticks & fi.Name
fi.CopyTo(destFile)
'open the new document
System.Diagnostics.Process.Start(destFile)