streamreader

Why is StreamReader and sr.BaseStream.Seek() giving Junk Characters even in UTF8 Encoding

妖精的绣舞 提交于 2020-05-16 08:14:46
问题 The abc.txt File Contents are ABCDEFGHIJ•XYZ Now, The Character Shown is Fine if I use this code (i.e. Seek to position 9), string filePath = "D:\\abc.txt"; FileStream fs = new FileStream(filePath, FileMode.Open); StreamReader sr = new StreamReader(fs, new UTF8Encoding(true), true); sr.BaseStream.Seek(9, SeekOrigin.Begin); char[] oneChar = new char[1]; char ch = (char)sr.Read(oneChar, 0, 1); MessageBox.Show(oneChar[0].ToString()); But if the SEEK position is Just after that Special Dot

Read HttpContent stream until a character limit using StreamReader

◇◆丶佛笑我妖孽 提交于 2020-04-13 06:33:09
问题 I am trying to convert the following code that reads the complete string response of a HttpContent into a string, to read only a certain maximum number of characters. Existing code: private static async Task<string> GetContentStringAsync(HttpContent content) { string responseContent = await content.ReadAsStringAsync().ConfigureAwait(false); return responseContent; } Code that I have now: private static async Task<string> GetContentStringAsync(HttpContent content, int ResponseContentMaxLength)

Getting path to the parent folder of the solution file using C#

回眸只為那壹抹淺笑 提交于 2020-02-20 07:14:50
问题 I am a beginner in C#, and I have a folder from which I am reading a file. I want to read a file which is located at the parent folder of the solution file. How do I do this? string path = ""; StreamReader sr = new StreamReader(path); So if my file XXX.sln is in C:\X0\A\XXX\ then read the .txt files in C:\X0\A\ . 回答1: Try this: string startupPath = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName,"abc.txt"); // Read the file as one

StreamReader is too greedy

此生再无相见时 提交于 2020-01-30 08:06:06
问题 I'm trying to process part of a text file, and write the remainder of the text file to a cloud blob using UploadFromStream . The problem is that the StreamReader appears to be grabbing too much content from the underlying stream, and so the subsequent write does nothing. Text file: 3 Col1,String Col2,Integer Col3,Boolean abc,123,True def,3456,False ghijkl,532,True mnop,1211,False Code: using (var stream = File.OpenRead("c:\\test\\testinput.txt")) using (var reader = new StreamReader(stream))

StreamReader is too greedy

放肆的年华 提交于 2020-01-30 08:05:50
问题 I'm trying to process part of a text file, and write the remainder of the text file to a cloud blob using UploadFromStream . The problem is that the StreamReader appears to be grabbing too much content from the underlying stream, and so the subsequent write does nothing. Text file: 3 Col1,String Col2,Integer Col3,Boolean abc,123,True def,3456,False ghijkl,532,True mnop,1211,False Code: using (var stream = File.OpenRead("c:\\test\\testinput.txt")) using (var reader = new StreamReader(stream))