streamreader

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

.NET C# - Random access in text files - no easy way?

泪湿孤枕 提交于 2019-11-28 06:23:53
I've got a text file that contains several 'records' inside of it. Each record contains a name and a collection of numbers as data. I'm trying to build a class that will read through the file, present only the names of all the records, and then allow the user to select which record data he/she wants. The first time I go through the file, I only read header names, but I can keep track of the 'position' in the file where the header is. I need random access to the text file to seek to the beginning of each record after a user asks for it. I have to do it this way because the file is too large to

HttpWebRequest and HttpWebResponse shows old data

微笑、不失礼 提交于 2019-11-28 06:06:49
问题 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

StreamWriter Multi Threading C#

巧了我就是萌 提交于 2019-11-28 05:30:51
问题 I would like to ask help on my code. I am a newbie and wanted to implement safe multi threading in writing to a text file. StreamWriter sw = new StreamWriter(@"C:\DailyLog.txt"); private void Update(){ var collection = Database.GetCollection<Entity>("products"); StreamReader sr = new StreamReader(@"C:\LUSTK.txt"); string[] line = sr.ReadLine().Split(new char[] { ';' }); while (!sr.EndOfStream) { line = sr.ReadLine().Split(new char[] { ';' }); t = delegate { UpdateEach(Convert.ToInt32(line[5])

C# Read webpage content Streamreader

≯℡__Kan透↙ 提交于 2019-11-28 04:16:05
问题 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) 回答1: Try a WebClient, it's easier and you don't have to worry about streams and rivers: using (var client = new WebClient()) { string

Httplistener and file upload

这一生的挚爱 提交于 2019-11-27 22:20:32
I am trying to retrieve an uploaded file from my webserver. As the client sends its files through a webform (random files), I need to parse the request to get the file out and to process it further on. Basically, the code goes as: HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; StreamReader r = new StreamReader(request.InputStream, System.Text.Encoding.Default); // this is the retrieved file from streamreader string file = null; while ((line = r.ReadLine()) != null){ // i read the stream till i retrieve the filename // get the file data out

Do I need to explicitly close the StreamReader in C# when using it to load a file into a string variable?

白昼怎懂夜的黑 提交于 2019-11-27 21:03:14
Example: variable = new StreamReader( file ).ReadToEnd(); Is that acceptable? No, this will not close the StreamReader. You need to close it. Using does this for you (and disposes it so it's GC'd sooner): using (StreamReader r = new StreamReader("file.txt")) { allFileText = r.ReadToEnd(); } Or alternatively in .Net 2 you can use the new File. static members, then you don't need to close anything: variable = File.ReadAllText("file.txt"); You should always dispose of your resources. // the using statement automatically disposes the streamreader because // it implements the IDisposable interface

How to skip first line while reading csv using streamreader

柔情痞子 提交于 2019-11-27 12:15:37
问题 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

Adding a Line to the Middle of a File with .NET

南笙酒味 提交于 2019-11-27 09:27:25
Hello I am working on something, and I need to be able to be able to add text into a .txt file. Although I have this completed I have a small problem. I need to write the string in the middle of the file more or less. Example: Hello my name is Brandon, I hope someone can help, //I want the string under this line. Thank you. Hopefully someone can help with a solution. Edit Alright thanks guys, I'll try to figure it out, probably going to just rewrite the whole file. Ok well the program I am making is related to the hosts file, and not everyone has the same hosts file, so I was wondering if

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

微笑、不失礼 提交于 2019-11-27 09:20:44
问题 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 =