Trying to open a docx file from a bytestream - file corruption error - vb.net

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)

Related

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

Retrieve full path of FTP file on drag & drop?

I can read the filename using next code when dragging a file from an Ftp folder browsed on Windows explorer.
But is there a way to retrieve the full Ftp path?
Private Sub DataGridView1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
Dim filename As String = ""
If e.Data.GetDataPresent("UniformResourceLocator") Then
Dim ioStream As System.IO.Stream = DirectCast(e.Data.GetData("FileGroupDescriptor"), System.IO.Stream)
Dim contents As Byte() = New [Byte](511) {}
ioStream.Read(contents, 0, 512)
ioStream.Close()
Dim sb As New System.Text.StringBuilder()
Dim i As Integer = 76
While contents(i) <> 0
sb.Append(CChar(ChrW(contents(i))))
i += 1
End While
filename = sb.ToString()
End If
End Sub
If the data dropped contains a UniformResourceLocator format, you can get the entire URL from that, for example:
Private Sub Form1_DragDrop(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
If e.Data.GetDataPresent("UniformResourceLocator") Then
Dim URL As String = New IO.StreamReader(CType(e.Data.GetData("UniformResourceLocator"), IO.MemoryStream)).ReadToEnd
End If
End Sub
It first checks to see if a UniformResourceLocator format exists, and if so, gets the data from e (the drag/drop argument), converts it to a MemoryStream, and passes that to a new StreamReader (for easy reading), then does a .ReadToEnd() to get the entire string.

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);

Unknown error wont open from computer. Basic

Sub ShowFileContents(ByVal strFileName As String)
Dim fs As New FileStream(strFileName, FileMode.Open, FileAccess.Read)
Dim TextFile As New StreamReader(fs)
Me.txtFileContents.Text = Nothing
Dim strLineOfText As String
Do While TextFile.Peek > -1
strLineOfText = TextFile.ReadLine()
Me.txtFileContents.Text = Me.txtFileContents.Text & strLineOfText & vbCrLf
Loop
TextFile.Close()
fs.Close()
End Sub
Why cant I open a file when I run program all it says is "Save" or "Cancel"
Why cant I open a file when I run program all it says is "Save" or "Cancel" What Im trying to do is open a file from my documents and display it in the textbox below. I have the save file working but not the open file
It looks like you could benefit from using the OpenFileDialog control rather than a textbox to get the file path you want to open.
Here's an example:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim ofp As New OpenFileDialog
If ofp.ShowDialog = Windows.Forms.DialogResult.OK Then
ShowFileContents(ofp.FileName)
End If
End Sub
Sub ShowFileContents(ByVal strFileName As String)
Dim TextFile As New StreamReader(strFileName)
txtFileContents.Text = Nothing
Do While Not TextFile.EndOfStream
txtFileContents.AppendText(TextFile.ReadLine() & vbCrLf)
Loop
TextFile.Close()
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)