filestream

Need help understanding Stream.Read()

ε祈祈猫儿з 提交于 2019-11-28 14:09:47
I am a little confused as to the steps of reading a file into buffer gradually. from the MSDN docs public abstract int Read( byte[] buffer, int offset, int count ) source from C# Examples FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); try { int length = (int)fileStream.Length; // get file length buffer = new byte[length]; // create buffer int count; // actual number of bytes read int sum = 0; // total number of bytes read // read until Read method returns 0 (end of the stream has been reached) while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)

FileStream with locked file

喜欢而已 提交于 2019-11-28 13:18:19
I am wondering if it's possible to get a readonly FileStream to a locked file? I now get an exception when I try to read the locked file. using (FileStream stream = new FileStream("path", FileMode.Open)) Thanks! You should try another constructor. They are documented at MSDN. This one looks like a bet: FileStream Constructor (String, FileMode, FileAccess, FileShare) MSDN Link FileAccess A constant that determines how the file can be accessed by the FileStream object. This gets the CanRead and CanWrite properties of the FileStream object. CanSeek is true if path specifies a disk file. FileShare

C# - How do I read and write a binary file?

邮差的信 提交于 2019-11-28 12:07:35
How do I read a raw byte array from any file, and write that byte array back into a new file? Marc Gravell (edit: note that the question changed; it didn't mention byte[] initially; see revision 1 ) Well, File.Copy leaps to mind; but otherwise this sounds like a Stream scenario: using (Stream source = File.OpenRead(inPath)) using (Stream dest = File.Create(outPath)) { byte[] buffer = new byte[2048]; // pick size int bytesRead; while((bytesRead = source.Read(buffer, 0, buffer.Length)) > 0) { dest.Write(buffer, 0, bytesRead); } } byte[] data = File.ReadAllBytes(path1); File.WriteAllBytes(path2,

Load and read a csv file with php

元气小坏坏 提交于 2019-11-28 11:47:06
I want to download some content in csv format. It is stored online in a csv file. I can not just read the content. I first have to download the file, open and then read it. Is there a way to open it directly? Or do I have to first load/upload it on my server and then open it like a classic file? Yes, it can open directly using fopen and fgetcsv However, this feature sometime is restricted for security concern, and you can read the details via the documentation http://php.net/manual/en/function.fopen.php http://php.net/manual/en/function.fgetcsv.php Another drawback of open directly, if the

How can I get uploaded file full path in C# 3.0?

故事扮演 提交于 2019-11-28 11:37:58
问题 In my ASP.NET MVC website, I have to read a txt file with some names and emails separeted by ';'. After that, I have to save each line of this txt file to database. Googling around, I've found some snippets, but in all of them I have to use the txt file path. But, how can I get this path? This file could be anywhere in a user's machine! Thanks!! 回答1: You cannot get the full path of an uploaded file. This would be a privacy violation for the user that uploaded the file. Instead, you'll need to

How Can I Save a Dynamic Array to a FileStream in Delphi?

眉间皱痕 提交于 2019-11-28 10:16:59
I have the following structures in Delphi 2009: type IndiReportIndi = record IndiName: string; NameNum: integer; ReportIndiName: string; end; var XRefList: array of IndiReportIndi; where XRefList is a dynamic array. I want to save XRefList to a FileStream. How do I do that AND include all the IndiName and ReportIndiName strings so that they will all be retrievable again when I later load from that FileStream? Use: http://code.google.com/p/kblib/ type IndiReportIndi = record IndiName: string; NameNum: integer; ReportIndiName: string; end; TXRefList = array of IndiReportIndi; var XRefList:

How to expose a sub section of my stream to a user

邮差的信 提交于 2019-11-28 07:33:31
问题 I have a stream that contains many pieces of data. I want to expose just a piece of that data in another stream. The piece of data I want to extract can often be over 100mb. Since I already have stream with the data in it it seems like a waste to copy that data to another stream and return that. What im looking for is a way to reference the data in the first stream while controlling how much of it the second stream can reference. Is this possible 回答1: There is a good implementation of this by

How to correctly open a FileStream for usage with an XDocument

会有一股神秘感。 提交于 2019-11-28 07:31:35
问题 I want to append some nodes to an xml document using Linq2XML. The file in question is being used by other processes and they should be able to read the file while I update it. So I came up with this solution, which obviously isn't the correct way (The method doc.Save() fails and says that another process is using the file): using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.ReadWrite, FileShare.Read)) { doc = XDocument.Load(new StreamReader(fs)); doc.Root.Add(entry);

How to convert stream results to string

假装没事ソ 提交于 2019-11-28 07:28:49
I want to convert the stream result output to string since I want to use it in Junit I think that I need to use the string writer but Im not sure how exactly to use it. StreamResult result = new StreamResult(new File("C:\\file.xml")); transformer.transform(source, result); Thanks Fedor Timo Hahn Have a look at and learn to use the javadocs of the StreamResult class ( http://java.sun.com/javase/6/docs/api/ ). One of the constructors of StreamResult takes a Writer object as a parameter. You will see that one of the sub-classes of Writer is StringWriter. So to obtain a string from what is written

Will a using clause close this stream?

落爺英雄遲暮 提交于 2019-11-28 06:56:24
问题 I've apparently worked myself into a bad coding habit. Here is an example of the code I've been writing: using(StreamReader sr = new StreamReader(File.Open("somefile.txt", FileMode.Open))) { //read file } File.Move("somefile.txt", "somefile.bak"); //can't move, get exception that I the file is open I thought that because the using clause explicitly called Close() and Dispose() on the StreamReader that the FileStream would be closed as well. The only way I could fix the problem I was having