streamreader

How to read mixed file of byte and string

大城市里の小女人 提交于 2019-12-10 19:50:01
问题 I've a mixed file with a lot of string line and part of byte encoded data. Example: --Begin Attach Content-Info: /Format=TIF Content-Description: 30085949.tif (TIF File) Content-Transfer-Encoding: binary; Length=220096 II*II* Îh ÿÿÿÿÿÿü³küìpsMg›Êq™Æ™Ôd™‡–h7ÃAøAú áùõ=6?Eã½/ô|û ƒú7z:>„Çÿý<þ¯úýúßj?å¿þÇéöûþ“«ÿ¾ÁøKøÈ%ŠdOÿÞÈ<,Wþ‡ÿ·ƒïüúCÿß%Ï$sŸÿÃÿ÷‡þåiò>GÈù#ä|‘ò:#ä|Š":#¢:;ˆèŽˆèʤV‘ÑÑÑÑÑÑÑÑÑçIþ×o(¿zHDDDDDFp'.Ñ:ˆR:aAràÁ¬LˆÈù!ÿÿï[ÿ¯Äàiƒ"VƒDÇ)Ê6PáÈê$9C”9C†‡CD¡pE@¦œÖ{i~Úý¯kköDœ4ÉU”8`ƒt!l2G --End Attach--

C# “using” blocks

风流意气都作罢 提交于 2019-12-10 15:08:10
问题 I've got something like the code below...someone here mentioned that the WebClient, Stream, and StreamReader objects could all benefit from using blocks. Two easy questions: 1: How would this little snippet look with using blocks? I've no problem with doing my own research so resource links are fine but it'd be quicker and easier to just see an example and I'll understand it from that. 2: I'd like to get in the habit of good coding standards, would help if I knew a little about the reasons

Reading and writing very large text files in C#

随声附和 提交于 2019-12-10 14:39:08
问题 I have a very large file, almost 2GB in size. I am trying to write a process to read the file in and write it out without the first row. I pretty much have been only able to read and write one line at a time which takes forever. I can open it, remove the first row and save it faster in TextPad, though that is still very slow. I use this code to get the number of records in the file: private long getNumRows(string strFileName) { long lngNumRows = 0; string strMsg; try { lngNumRows = 0; using

Why is my Stream not Readable?

强颜欢笑 提交于 2019-12-10 14:34:44
问题 This is a sequel to my question here about trimming the fat off a log file. I have this code: private readonly FileStream _fileStream; private readonly StreamWriter _streamWriter; . . . const int MAX_LINES_DESIRED = 1000; string uriPath = GetExecutionFolder() + "\\Application.log"; string localPath = new Uri(uriPath).LocalPath; if (!File.Exists(localPath)) { File.Create(localPath); } _fileStream = File.OpenWrite(localPath); // First, remove the earliest lines from the file if it's grown too

Get length of Streamreader

独自空忆成欢 提交于 2019-12-10 14:28:17
问题 How can I get the length of a StreamReader , as I know nothing will be written to it anymore. I thought that maybe I could pass all the data to a MemoryStream , which has a method called Length , but I got stuck on how to append a byte[] to a MemoryStream . private void Cmd(string command, string parameter, object stream) { StreamWriter writer = (StreamWriter)stream; StreamWriter input; StreamReader output; Process process = new Process(); try { process.StartInfo.UseShellExecute = false;

Extracting the first 10 lines of a file to a string

做~自己de王妃 提交于 2019-12-10 12:32:54
问题 public void get10FirstLines() { StreamReader sr = new StreamReader(path); String lines = ""; lines = sr.readLine(); } How can I get the first 10 lines of the file in the string? 回答1: Rather than using StreamReader directly, use File.ReadLines which returns an IEnumerable<string> . You can then use LINQ: var first10Lines = File.ReadLines(path).Take(10).ToList(); The benefit of using File.ReadLines instead of File.ReadAllLines is that it only reads the lines you're interested in, instead of

Why is my HttpWebRequest POST method to my WebAPI server failing?

百般思念 提交于 2019-12-10 10:22:48
问题 I've successfully received data from my WebAPI project ("GET"), but my attempt to Post is not working. Here is the relevant server/WebAPI code: public Department Add(Department item) { if (item == null) { throw new ArgumentNullException("item"); } departments.Add(item); return item; } ...which fails on the "departments.Add(item);" line, when this code from the client is invoked: const string uri = "http://localhost:48614/api/departments"; var dept = new Department(); dept.Id = 8; dept

Consume a StreamReader stream .NET from a web request as it streams?

﹥>﹥吖頭↗ 提交于 2019-12-10 09:27:18
问题 I'm experimenting with the Twitter streaming api, and am trying to open a stream for a user to consume events as they happen. I'm using a standard set of classes for making REST api calls to twitter. When using https://userstream.twitter.com/2/user.json in a "GET" call the response stream never ends... I'm opening a StreamReader and reading the response as I would with any other REST call to Twitter. This is probably obvious to others, but how do I "consume" this stream... Is there a way to

What are the default values for StreamReader?

故事扮演 提交于 2019-12-08 16:07:31
问题 I need to use this constructor public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen) in order to set leaveOpen to true . And in order to do that I need to set the other parameters as well ( Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize ). I want to use StreamReader as it is so I do not want to give some random values. What are the default values for these parameters? By the way, I know that I can

C# Stream Reader adding \\n to XML

拈花ヽ惹草 提交于 2019-12-08 10:48:28
I use the StreamReader class to obtain XML for my GeoCoding process from Google. StreamReader srGeoCode = new StreamReader(WebRequest.Create(Url).GetResponse().GetResponseStream()); String GeoCodeXml = srGeoCode.ReadToEnd(); XmlDocument XmlDoc = new XmlDocument(); GeoCode oGeoCode = new GeoCode(); XmlDoc.Load(GeoCodeXml); I get XML back but it adds \n and other extras to the XML <?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<kml xmlns=\"http://earth.google.com/kml/2.0\"><Response>\n <name> I have the same code in VB and it does not do this. I can successfully GeoCode my information using the VB