memorystream

Using MemoryStream to write out to XML

若如初见. 提交于 2019-11-30 03:25:17
问题 I have noticed two different approaches to writing out data to an XML file (error handling omitted for brevity). The first method has you build the XML document and then simply save the XML to a file: using (XmlWriter writer = XmlWriter.Create(fileName)) { writer.WriteStartDocument(true); writer.WriteStartElement("parentelement"); writer.WriteEndElement(); writer.WriteEndDocument(); } The second method has you create a MemoryStream, and then save the MemoryStream to a file: XmlWriterSettings

.NET MemoryStream - Should I set the capacity?

故事扮演 提交于 2019-11-30 01:03:07
问题 This is probably a really simple question, I think all that I am after is Best Practice for declaring a new MemoryStream What is the difference between the following 2 samples: MemoryStream myStream = new MemoryStream(0x10000); or MemoryStream myStream = new MemoryStream(); Obviously, I know that the first example set the initial capacity of the stream. But, both of these have an automatically resizable capacity. I there any reason why I should use one method as opposed to the other? 回答1:

iTextSharp is producing a corrupt PDF with Response

烂漫一生 提交于 2019-11-29 14:51:12
i tryed both, but still not working iTextSharp + FileStream = Corrupt PDF file iTextSharp is producing a corrupt PDF using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream()) { //abre o documento para poder editar document.Open(); //Adiciona os campos de assinatura document.Add(Assinatura()); //fecha o documento ao finalizar a edição document.Close(); //Prepara o download byte[] bytes = memoryStream.ToArray(); memoryStream.Close(); Response.Clear(); Response.ContentType = "image/pdf"; //Response.ContentType = "application/pdf"; Response.AddHeader("Content-Disposition",

MemoryStream must be explicitely be disposed?

╄→尐↘猪︶ㄣ 提交于 2019-11-29 10:50:07
As MemoryStream is an unmanaged resource does it always have to be disposed? Given: 1) A method is invoked. 2) A MemoryStream object is created (MemoryStream ms = new MemoryStream();). 3) An exception occurs and is caught from the invoking classes. The reference on the MemoryStream object is therefore lost. Does this scenario need a try/finally-block (or using-statement)? In general, all disposable objects must always be disposed. However, MemoryStream doesn't actually need to be disposed, since it doesn't have any unmanaged resources. (It's just a byte[] and an int ) The only reason it's

Reset or Clear .NET MemoryStream

▼魔方 西西 提交于 2019-11-29 10:32:05
问题 The .NET MemoryStream does not appear to have a .Reset or .Clear method. I was thinking of using the following code to accomplish this: ms.Seek(0, IO.SeekOrigin.Begin) ms.SetLength(0) What is the proper way to clear or reset an existing .NET MemoryStream? 回答1: The memorystream does not have a reset/clear method because it would be redundant. By setting it to zero length you clear it. Of course you could always do: memoryStream = new MemoryStream(memoryStream.Capacity()); This would yield you

How can I pass MemoryStream data to unmanaged C++ DLL using P/Invoke

末鹿安然 提交于 2019-11-29 09:53:48
I need your help with the following scenario: I am reading some data from hardware into a MemoryStream (C#) and I need to pass this data in memory to a dll implemented in unmanaged C++ (using pointer ??). The data read (into stream) is very large (megabytes). I understand that I can P/Invoke this dll but what I am not sure is how to pass the pointer / reference of the stream data to the C++ API ? I must admit I am confused as I am new to C# - do I need to use unsafe / fixed since data is large or these are irrelevant as MemoryStream object is managed by GC ? Some example code / detailed

MemoryStream in Using Statement - Do I need to call close()

萝らか妹 提交于 2019-11-29 09:03:35
When using a memory stream in a using statement do I need to call close? For instance is ms.Close() needed here? using (MemoryStream ms = new MemoryStream(byteArray)) { // stuff ms.Close(); } No, it's not. using ensures that Dispose() will be called, which in turn calls the Close() method. You can assume that all kinds of Streams are getting closed by the using statement. From MSDN : When you use an object that accesses unmanaged resources, such as a StreamWriter, a good practice is to create the instance with a using statement. The using statement automatically closes the stream and calls

How to run unmanaged executable from memory rather than disc

本秂侑毒 提交于 2019-11-28 23:55:01
I want to embed a command-line utility in my C# application, so that I can grab its bytes as an array and run the executable without ever saving it to disk as a separate file (avoids storing executable as separate file and avoids needing ability to write temporary files anywhere). I cannot find a method to run an executable from just its byte stream. Does windows require it to be on a disk, or is there a way to run it from memory? If windows requires it to be on disk, is there an easy way in the .NET framework to create a virtual drive/file of some kind and map the file to the executable's

Composite Stream Wrapper providing partial MemoryStream and full original Stream

回眸只為那壹抹淺笑 提交于 2019-11-28 13:46:51
Does anyone know of a composite stream solution that will pre-load the first portion of a Stream in to a MemoryStream and keep the remainder as the original Stream which will be accessed when subsequent parts are required as necessary? I should imagine some wrapper class would implement the Stream interface and transparently juggle the access between the two streams depending upon which part is accessed. I'm hoping this is a solution someone may have solved before, maybe to optimize performance of reading a large FileStream. In my case I'm trying to get around a Windows Phone 8 bug reading

Merge memorystreams to one iText document

感情迁移 提交于 2019-11-28 10:18:30
I have four MemoryStreams of data that I want to merge and then open the pdfDocument, without creating a single file. It's possible to write them down to files and then merge them but that would be bad practice and that can also cause a few issues so I want to avoid that. However, I can not find a way to merge the MemoryStreams with iText5 for .NET. Right now, this is how I do it with files: private static void ConcatenateDocuments() { var stream = new MemoryStream(); var readerFrontPage = new PdfReader(Folder + FrontPageName); var readerDocA = new PdfReader(Folder + docA); var readerDocB =