Asp.net Upload File to Directory Memory Stream
MemoryStream
The MemoryStream creates a stream whose backing store is retentivity. Actually, it represents a pure, in-retentivity stream of data. Retentiveness is much faster when compared to disk or network accesses.
The following section explains :
# MemoryStream to File
# MemoryStream to String
MemoryStream to FileStream
With MemoryStream, you can act upon the byte[] stored in retentiveness rather than a file or other resource. Utilise a byte[] considering it is a stock-still sized object making it easier for memory allotment and cleanup and holds relatively no overhead, especially since y'all don't need to use the functions of the MemoryStream.
Save Stream to File
C# MemoryStream to a file
using Arrangement; using Arrangement.Text; using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { cord memString = "Retention test cord !!"; // convert string to stream byte[] buffer = Encoding.ASCII.GetBytes(memString); MemoryStream ms = new MemoryStream(buffer); //write to file FileStream file = new FileStream("d:\\file.txt", FileMode.Create, FileAccess.Write); ms.WriteTo(file); file.Close(); ms.Close(); } } } Save and load MemoryStream to a file
VB.Internet MemoryStream to a file
Imports Arrangement.IO Imports Organisation.Text Public Grade Form1 Private Sub Button1_Click(ByVal sender Equally System.Object, ByVal east As System.EventArgs) Handles Button1.Click Dim memString As String = "Retentiveness test string !!!" ' convert string to stream Dim buffer As Byte() = Encoding.ASCII.GetBytes(memString) Dim ms Equally New MemoryStream(buffer) 'write to file Dim file As New FileStream("d:\file.txt", FileMode.Create, FileAccess.Write) ms.WriteTo(file) file.Shut() ms.Close() End Sub End Class Save MemoryStream to a String
The following programme shows how to Read from memorystream to a cord. Steps follows..
StreamWriter sw = new StreamWriter(memoryStream); sw.WriteLine("Your cord to Memoery"); This string is currently saved in the StreamWriters buffer. Flushing the stream will force the string whose backing store is memory (MemoryStream).
How do you become a cord from a MemoryStream
Side by side step is to read this string from memorystream.
ms.Position = 0; StreamReader sr = new StreamReader(ms); string myStr = sr.ReadToEnd();
The StreamReader will read from the current position of the MemoryStream which is currently ready at the stop of the string earlier we just wrote to information technology. We take to fix the position to 0 in order to read from the start.
Save MemoryStream to a String - C#
using System; using Organization.Windows.Forms; using System.IO; namespace WindowsFormsApplication1 { public fractional class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs due east) { using (MemoryStream ms = new MemoryStream()) { StreamWriter sw = new StreamWriter(ms); sw.WriteLine("Hello Earth !!"); sw.Flush(); ms.Position = 0; StreamReader sr = new StreamReader(ms); string myStr = sr.ReadToEnd(); MessageBox.Evidence(myStr); } } } } Save MemoryStream to a String - VB.Internet
Imports Arrangement.IO Public Class Form1 Private Sub Button1_Click(ByVal sender Every bit Organization.Object, ByVal due east Equally Organization.EventArgs) Handles Button1.Click Using ms Equally New MemoryStream() Dim sw As New StreamWriter(ms) sw.WriteLine("How-do-you-do World !!") sw.Flush() ms.Position = 0 Dim sr As New StreamReader(ms) Dim myStr As String = sr.ReadToEnd() MessageBox.Evidence(myStr) Stop Using End Sub End Class There is another methos as well assistance united states to read from Memory stream.
Encoding.ASCII.GetString(ms.ToArray());
C# Source Code
using System; using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } individual void button1_Click(object sender, EventArgs due east) { using (MemoryStream ms = new MemoryStream()) { StreamWriter sw = new StreamWriter(ms); sw.WriteLine("Hello World !!!"); sw.Flush(); string myStr = Encoding.ASCII.GetString(ms.ToArray ()); MessageBox.Show(myStr); } } } } VB.Net Source Code
Imports System.IO Imports System.Text Public Class Form1 Individual Sub Button1_Click(ByVal sender As System.Object, ByVal e As Organisation.EventArgs) Handles Button1.Click Using ms As New MemoryStream() Dim sw Equally New StreamWriter(ms) sw.WriteLine("Hello World !!!") sw.Flush() Dim myStr As String = Encoding.ASCII.GetString(ms.ToArray()) MessageBox.Show(myStr) Stop Using Stop Sub End Course Next.....Parse XML - XmlReader
Source: http://net-informations.com/q/faq/memory.html
0 Response to "Asp.net Upload File to Directory Memory Stream"
ارسال یک نظر