I'm trying to implement a library "Boleto2Net", I saw this video in YouTube and I downloaded his example... But I'm having problem with this line:
'GERA ARQUIVO DE REMESSA
Dim st As New MemoryStream
Dim arquivo As New FileStream(Application.StartupPath & "\remessa.txt", FileMode.Create, FileAccess.ReadWrite)
Dim remessa = New ArquivoRemessa(objBoletos.Banco, TipoArquivo.CNAB400, 500) ' If I coment this line, the error doesn't occurs
remessa.GerarArquivoRemessa(objBoletos, st) ' If I coment this line, the error doesn't occurs
st.WriteTo(arquivo)
arquivo.Close()
st.Close()
I'm getting Cannot access a closed Stream error, I really doesn't know how memory streans works and how to deal with it, I tried to find an "Open" or "Close" command for memorystream, but there's none.
Related
I am having problems trying to read a text file which is open by another process.
After searching SO I have found a few similar questions albeit in C# and nor VB.Net which seem to refer to the fact that FileShare.ReadWrite is the key to getting this to work but yet I am still struggling with it.
This is what I have so far but nothing is appearing in TextBox1.
Dim logFileStream As FileStream = New FileStream("C:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Dim logFileReader As StreamReader = New StreamReader(logFileStream)
While Not logFileReader.EndOfStream
Dim line As String = logFileReader.ReadLine()
TextBox1.Text = line
End While
logFileReader.Close()
logFileStream.Close()
My goal is to just use the last 2 lines of what's in the file c:\test.txt and display those contents into a Label but I guess I first need to read and show the content before I can start to look at just extracting the last 2 lines.
Update:
After re-visiting the MS Docs, I have rearranged the code as below and I can now seem to read the open file into a TextBox
Dim strLogFilePath As String
Dim LogFileStream As FileStream
Dim LogFileReader As StreamReader
Dim strRowText As String
strLogFilePath = "C:\DSD\DSDPlus.srt"
LogFileStream = New FileStream(strLogFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
LogFileReader = New StreamReader(LogFileStream)
strRowText = LogFileReader.ReadToEnd()
TextBox1.Text = strRowText
LogFileReader.Close()
LogFileStream.Close()
I have an xps file. When I try to print this file directly, I can do it without any error with my below code:
Dim defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue
Dim xpsPrintJob As PrintSystemJobInfo = defaultPrintQueue.AddJob("test", "C:\Temp\test.xps", False)
However, If I get this file from a web service as a byte array and save it as an xps file I cannot print it.
My save byte array codes are below:
FS = New IO.FileStream("C:\Temp\test.xps", FileMode.Create)
FS.Write(arrayByte, 0, arrayByte.Length)
FS.Close()
or this code:
File.WriteAllBytes("c:\Temp\test.xps", arrayByte)
When I try to print test.xps, I'm getting the error:
An unhandled exception of type 'System.Printing.PrintJobException'
occurred in System.Printing.dll
Additional information: An exception occurred while creating print job
information. Check inner exception for details.
Have can I handle this problem? Is anyone has any idea?
By the way, there is no need a web service.
Please see my below code.
You can try this any xps file.
Firstly, I'm assinging the file as byte array
Then, I'm saving the byte array as an xps file.
First XPS file is working but second one isn't working
Dim FS As FileStream
FS = File.Open("C:\Temp\test2.xps", FileMode.Open, FileAccess.Read)
Dim bByte(FS.Length) As Byte
FS.Read(bByte, 0, FS.Length)
FS.Close()
File.WriteAllBytes("c:\Temp\test2byte.xps", bByte)
Dim defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue
'This is working
Dim xpsPrintJob1 As PrintSystemJobInfo = defaultPrintQueue.AddJob("Test", "C:\Temp\test2.xps", False)
'This is not working
Dim xpsPrintJob2 As PrintSystemJobInfo = defaultPrintQueue.AddJob("Test", "C:\Temp\test2byte.xps", False)
I've solve the problem.
I've changed the below code:
Dim FS As FileStream
FS = File.Open("C:\Temp\test2.xps", FileMode.Open, FileAccess.Read)
Dim bByte(FS.Length) As Byte
FS.Read(bByte, 0, FS.Length)
FS.Close()
as
Dim bByte() As Byte = File.ReadAllBytes("C:\Temp\test2.xps")
and the problem has been solved.
I need download a CSV file and then read it. Here is my code:
tickerValue = "goog"
Dim strURL As String = "http://ichart.yahoo.com/table.csv?s=" & tickerValue
Dim strBuffer As String = RequestWebData(strURL)
Using streamReader = New StreamReader(strBuffer)
Using reader = New CsvReader(streamReader)
I keep getting this error: An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll Additional information: Illegal characters in path.
What am I doing wrong?
Additional Info
In another part of my program I use this code and it works fine.
Address = http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=AMEX&render=download
Dim strBuffer As String = Historical_Stock_Prices.RequestWebData(Address)
Using streamReader = New StringReader(strBuffer)
Using reader = New CsvReader(streamReader)
Isn't my second code the same concept as my problem code?
you are giving it, essentially, a web url. somewhere in your code, it does not support the web url. it could be the streamreader. it could be the CsvReader.
what line of code does this point to?
the best bet is to save the file TO DISK, then read from disk.
UPDATE
here is an example to SAVE to disk:
using writer as new StreamWriter("C:\Test.csv")
writer.Write(strBuffer)
writer.Close()
end using
here is an example to READ from disk:
using strReader as new StreamReader("C:\Test.csv")
' this code is presumably how it works for reading into the CsvReader:
using reader as new CsvReader(strReader)
' now do your thing
end using
strReader.Close()
end using
on my .xsd file where I create the datatable for use in my Crystal Report (buit in with Visual Studio 2005), I have included a column (which is datatatype = 'Byte') that is supposed to hold the image for my crystal report.
However, there is always an error thrown at where I've typed COMMENT1, saying that the file is being used by another process. The file I am accessing is a mapped drive to our server.
when I try to ignore the error and press on continue, the report does show up..but without the image it was supposed to be with.
below is my complete syntax
Dim x As New Reporter
x.MdiParent = Magulang
Dim dt As New DataTable
dt.Columns.Add("EmpNo")
dt.Columns.Add("FullName")
dt.Columns.Add("Project")
dt.Columns.Add("SSS")
dt.Columns.Add("TIN")
dt.Columns.Add("EmergencyPerson")
dt.Columns.Add("EmergencyAddress")
dt.Columns.Add("EmergencyContact")
dt.Columns.Add("PicNya", System.Type.[GetType]("System.Byte[]"))
Dim middleinitial As String = MiddleName.Text.Substring(0, 1)
Dim fs As FileStream
Dim br As BinaryReader
If File.Exists(PicPath.Text) Then
'COMMENT1 : there is error here, saying that file is used by another process
fs = New FileStream(PicPath.Text, FileMode.Open)
Else
fs = New FileStream(AppDomain.CurrentDomain.BaseDirectory + "NoPhoto.jpg", FileMode.Open)
End If
br = New BinaryReader(fs)
Dim imgbyte As Byte() = New Byte(fs.Length) {}
imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)))
MsgBox(imgbyte.ToString)
dt.Rows.Add(EmpID.Text, FirstName.Text & " " & middleinitial & ". " & LastName.Text, Project.Text, SSS.Text, TIN.Text, EmergencyContact.Text, "", EmergencyContactNo.Text, imgbyte)
br.Close()
fs.Close()
Dim rptdoc As CrystalDecisions.CrystalReports.Engine.ReportDocument
rptdoc = New HRISID1
rptdoc.SetDataSource(dt)
x.CrystalReportViewer1.ReportSource = rptdoc
ProgressForm.Hide()
x.Show()
What can I do to resolve COMMENT1? if resolved, will the image finally output in my crystal report? Am I on the right track, if I wanted to output images on my computer?
For now, we have resolved to instead pass each Data into a seperate form, then use the PRINTFORM object included with the VB Powerpacks. This is to make sure the image is also printed.
I can't help but feel this isn't the proper solution though, so if anyone can still contribute it's very much welcome.
I am making an encryption program that also functions as a notepad (CryptoMatic). There is a Open function with the following code:
Dim r As New StreamReader(filename)
Dim text As String = r.ReadToEnd
txtFileContents.text = text
r.Dispose()
r.Close()
Now there is the save and save as button
the save button checks if the file had been saved previously or not. If yes then I am trying to write to that file with the following code:
Dim myFileStream As New System.IO.FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.None)
myFileStream.Lock(0, 100)
Dim myWriter As New System.IO.StreamWriter(myFileStream)
myWriter.WriteLine(text)
myWriter.Flush()
myWriter.Close()
myFileStream.Close()
myFileStream.Unlock(0, 100)
Now when i press the save button it gives the following error:
The process cannot access the file 'C:\Users\VisualTech\Documents\Visual Studio 2010\Projects\CryptoMatic Update\CryptoMatic Update\bin\Debug\text.txt' because it is being used by another process.
and the application stops, can anyone please help me?
I have tried your code above and have noticed two things:
You don't need a myFileStream.Lock call because you already open it
with FileShare.None
You can't call myFileStream.Unlock(0, 100) after the call to
myFileStream.Close()
So I have rewritten your code in this way.
Sub Main
Dim text = "This is a text to write in the stream"
Using myFileStream As New System.IO.FileStream("D:\temp\testlock.txt", FileMode.Append, FileAccess.Write, FileShare.None)
Using myWriter As New System.IO.StreamWriter(myFileStream)
myWriter.WriteLine(text)
End Using
End Using
End Sub