|
|
|
|
|
|
|
Author Rank :
|
|
|
Page Views :
|
1556
|
|
Downloads :
|
0
|
|
Rating :
|
Rate it
|
|
Level :
|
Beginner
|
|
|
HTML clipboard
MemoryStream Class:
A memory stream is created from an array of unsigned bytes rather than from a
file or other stream. Memory streams are used as temporary, in-memory storage
(temporary buffers) in lieu of creating temporary files. This stream is highly
optimized for speed since the data is stored in memory and the processor can
easily access it. Memory streams should be used to store frequently accessed
data.
The Read and Write methods of the MemoryStream class read and write from an
internal buffer that is created when the memory stream is created. The example
shown in Listing 6.9 uses the MemoryStream class to add a custom signature at
the end of the specified file.
Listing 6.9: MemoryStream Reading and Writing Example
Imports
System
Imports
System.IO
Imports
System.Text
Public
Class MemStream
Public
Shared Sub Main(ByVal
args As String())
'Check the number or arguments
If args.Length < 1
Then
Console.WriteLine("Usage:
MemStream <sourcefile>")
Return
End If
Try
'Get the current date
Dim dt
As DateTime =
DateTime.Now
Dim tag As
String = "This file
was signed on " + dt.ToShortDateString()
'Get a byte array from the string
Dim tagarray
As Byte() =
System.Text.Encoding.ASCII.GetBytes(tag.ToCharArray())
'Construct a memory stream with the byte
'array as a parameter
Dim mstream
As New
MemoryStream(tagarray)
'Open a FileStream on the source file
Dim fout
As New
FileStream(args(0),
FileMode.Open,
FileAccess.Write)
'Seek to the end of the file
fout.Seek(0, SeekOrigin.[End])
Dim buffer
As [Byte]() =
New [Byte](tagarray.Length) {}
Console.WriteLine("Starting
to write signature")
'Read the contents of the MemoryStream
into a buffer
Dim n
As Integer =
mstream.Read(buffer, 0, buffer.Length
'Write the buffer to the file
fout.Write(buffer, 0, n)
'Close the streams
mstream.Close()
fout.Close()
Console.WriteLine("Signature
Written")
Catch e As
IOException
Console.WriteLine("An
IO Exception Occurred :", e)
Catch oe As
Exception
Console.WriteLine("An
Exception Occurred :", oe)
End Try
Console.ReadLine()
End
Sub
End
Class
In this example, a MemoryStream object is created and a byte array–containing
signature is stored in the memory stream's buffer. Then a file stream is opened
on the source file and the Seek method is used to seek to the end of the file.
Once positioned at the end of the file, the code gets the contents of the memory
stream and writes the contents to the file stream.
Conclusion
Hope this article would have helped you in understanding MemoryStream Class in
VB.NET.
|
|
Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post
Here.
|
|
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
|
|
|
|
|
|
|
|
|
|
|
|
|