streamreader

Will a using clause close this stream?

情到浓时终转凉″ 提交于 2019-11-29 13:07:23
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 was by changing the above block to this: using(FileStream fs = File.Open("somefile.txt", FileMode.Open))

Reading large file in chunks c#

╄→尐↘猪︶ㄣ 提交于 2019-11-29 12:54:38
问题 I want to read very large file (4GBish) chunk by chunk. I am currently trying to use a StreamReader and the Read() read method. The syntax is: sr.Read(char[] buffer, int index, int count) Because the index is an int it will overflow in my case. What should i use instead? 回答1: The index is the starting index of buffer not the index of file pointer, usually it would be zero. On each Read call you will read characters equal to the count parameter of Read method. You would not read all the file

HttpWebRequest and HttpWebResponse shows old data

萝らか妹 提交于 2019-11-29 12:27:54
After updating the data, when the webservice is called, it still fetches old data. New data is loaded only when I logout of the app and then login again. protected async override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) { base.OnNavigatedTo(e); parameterValue = this.NavigationContext.QueryString["parameter"]; Uri UserDetailUrl = new Uri(Constants.WebService.ws_single_user + "?user_id=" + parameterValue); HttpWebRequest UserDetailRequest = (HttpWebRequest)HttpWebRequest.Create(UserDetailUrl); HttpWebResponse UserDetailResponse = (HttpWebResponse)await

C# Read webpage content Streamreader

允我心安 提交于 2019-11-29 10:55:19
I need to read the content of a webpage in streamreader like www.example.com <test> <sample></sample> </test> i got this: System.IO.StreamReader StreamReader1 = new System.IO.StreamReader("www.example.com"); string test = StreamReader1.ReadToEnd(); but i then i get this error code Attempt to access the method failed: System.IO.StreamReader..ctor(System.String) Darin Dimitrov Try a WebClient , it's easier and you don't have to worry about streams and rivers: using (var client = new WebClient()) { string result = client.DownloadString("http://www.example.com"); // TODO: do something with the

Read text file from C# Resources

心不动则不痛 提交于 2019-11-29 01:22:07
I need to read a file from my resources and add it to a list. my code: private void Form1_Load(object sender, EventArgs e) { using (StreamReader r = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("myProg.myText.txt"))) { //The Only Options Here Are BaseStream & CurrentEncoding } } Ive searched for this and only have gotten answers like "Assembly.GetExecutingAssembly...." but my program doesnt have the option of Assembly.? Try something like this : string resource_data = Properties.Resources.test; List<string> words = resource_data.Split(new[] {Environment.NewLine},

How to skip first line while reading csv using streamreader

蹲街弑〆低调 提交于 2019-11-28 19:22:01
I have my following code to read values from csv file and do some processing. I would like to skip the first row of the input csv file as it contains header text but I'd want to add it back after the processing is done. List<string> values = new List<string>(); using (StreamReader sr = new StreamReader(filePath)) { while (sr.Peek() != -1) { string line = sr.ReadLine(); List<string> lineValues = line.Split(',').ToList(); var tempMinInt = 1; var tempValue = 1; var tempValInt = Convert.ToInt32(lineValues[4]); if (lineValues[3] == "1876") { if (tempValInt % 60 != 0) { tempMinInt = (tempValInt / 60

How do you send an HTTP Get Web Request in Python? [duplicate]

不羁岁月 提交于 2019-11-28 15:42:20
This question already has an answer here: What is the quickest way to HTTP GET in Python? 11 answers I am having trouble sending data to a website and getting a response in Python. I have seen similar questions, but none of them seem to accomplish what I am aiming for. This is my C# code I'm trying to port to Python: static void Request(Uri selectedUri) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(selectedUri); request.ServicePoint.BindIPEndPointDelegate = BindIPEndPointCallback; request.Method = "GET"; request.Timeout = (int)Timeout.TotalMilliseconds; request.ReadWriteTimeout

StreamReader and reading an XML file

為{幸葍}努か 提交于 2019-11-28 12:06:41
I get a response from a web-server using StreamReader... now I want to parse this response (it's an XML document file) to get its values, but every time I try to do it I get a error: Root element is missing. If I read the same XML file directly, the file is well formatted and I can read it. This is the stream: WebResponse response = webRequest.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader responseReader = new StreamReader(responseStream); string responseString = responseReader.ReadToEnd(); And this is how I try to read the XML file: XmlDocument xmlDoc = new

C# - StreamReader.ReadLine does not work properly!

。_饼干妹妹 提交于 2019-11-28 09:14:12
Simply I have been trying to implement what BufferedStreamReader does in Java. I have a socket stream open and just want to read it in a line-oriented fashion - line by line. I have the following server-code. while (continueProcess) { try { StreamReader reader = new StreamReader(Socket.GetStream(), Encoding.UTF8); string command = reader.ReadLine(); if (command == null) break; OnClientExecute(command); } catch (Exception e) { Console.WriteLine(e.ToString()); } } And the following client-code: TcpClient tcpClient = new TcpClient(); try { tcpClient.Connect("localhost", serverPort); StreamWriter

How to string multiple TextReaders together?

≡放荡痞女 提交于 2019-11-28 08:39:16
问题 I have 3 TextReaders -- a combination of StreamReaders and StringReaders. Conceptually, the concatenation of them is a single text document. I want to call a method (not under my control) that takes a single TextReader. Is there any built-in or easy way to make a concatenating TextReader from multiple TextReaders? (I could write my own TextReader subclass, but it looks like a fair amount of work. In that case, I'd just write them all out to a temp file and then open it with a single