streamreader

C# - FileStream: both lock a file and at the same time be able to read it without truncating it and write it with truncating it

廉价感情. 提交于 2019-12-01 17:26:12
I suppose my title isn't that clear. I'll try to explain: I can write and read a file using a FileStream FileStream fs = new FileStream("C:\\Users\\Public\\text.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); private void button1_Click(object sender, EventArgs e) { fs.Seek(0,0); StreamReader sr = new StreamReader(fs); textbox.Text = sr.ReadToEnd(); } private void button2_Click(object sender, EventArgs e) { StreamWriter sw = new StreamWriter(fs); sw.Write(textbox.Text); sw.Flush(); } This way other programs can't use the file, but I also can't delete content. Writing to it

Zip file is getting corrupted after downloading from server in C#

对着背影说爱祢 提交于 2019-12-01 14:08:39
request = MakeConnection(uri, WebRequestMethods.Ftp.DownloadFile, username, password); response = (FtpWebResponse)request.GetResponse(); Stream responseStream = response.GetResponseStream(); //This part of the code is used to write the read content from the server using (StreamReader responseReader = new StreamReader(responseStream)) { using (var destinationStream = new FileStream(toFilenameToWrite, FileMode.Create)) { byte[] fileContents = Encoding.UTF8.GetBytes(responseReader.ReadToEnd()); destinationStream.Write(fileContents, 0, fileContents.Length); } } //This part of the code is used to

How reading messages from Server? (tcp)

可紊 提交于 2019-12-01 12:13:49
Client write to server - server read. and Server write to client - client not read. Server: using System; using System.Text; using System.Net; using System.Net.Sockets; using System.IO; class SocketServer { public static void Main() { StreamReader streamReader; NetworkStream networkStream; TcpListener tcpListener = new TcpListener(5555); tcpListener.Start(); Console.WriteLine("The Server has started on port 5555"); Socket serverSocket = tcpListener.AcceptSocket(); try { if (serverSocket.Connected) { Console.WriteLine("Client connected"); networkStream = new NetworkStream(serverSocket);

How reading messages from Server? (tcp)

拈花ヽ惹草 提交于 2019-12-01 11:02:48
问题 Client write to server - server read. and Server write to client - client not read. Server: using System; using System.Text; using System.Net; using System.Net.Sockets; using System.IO; class SocketServer { public static void Main() { StreamReader streamReader; NetworkStream networkStream; TcpListener tcpListener = new TcpListener(5555); tcpListener.Start(); Console.WriteLine("The Server has started on port 5555"); Socket serverSocket = tcpListener.AcceptSocket(); try { if (serverSocket

C# Client/Server: using Streamreader/writer

拟墨画扇 提交于 2019-12-01 10:38:14
I'm relatively new to C# but here goes: I am developing a remote file service client/server console application in C# which is supposed to exchange messages using synchronous sockets. One of the main problems (even thought it might seem simple) is to return a string from the server, to the client using streamreader/streamwriter. The application user a command line interface with options (from a switch statement) to execute actions. I.e. typing 1 and enter would execute the code to send the string from the server to the client. Below is a sample code from the client: try { using (TcpClient

Stream Reader process cannot access file because its in use by another process [duplicate]

馋奶兔 提交于 2019-12-01 09:36:23
This question already has an answer here: How can I read a file even when getting an “in use by another process” exception? 4 answers My application parses log files but when trying to parse the current day's file I get an error stating that the file is being used by another process. This log file is currently being written to and can be accessed through notepad but not through my application. Current Code: Stream stream = new FileStream(fileToRead, FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(stream); Also tried this but had no luck: Stream stream = new FileStream

Execute Process Chain

痞子三分冷 提交于 2019-12-01 07:31:40
public void ExecuteProcessChain(string[] asProcesses, string sInRedirect, string sOutRedirect) { Process p1 = new Process(); p1.StartInfo.UseShellExecute = false; p1.StartInfo.RedirectStandardOutput = true; p1.StartInfo.FileName = asProcesses[0]; p1.Start(); StreamReader sr = p1.StandardOutput; string s, xxx = ""; while ((s = sr.ReadLine()) != null) Console.WriteLine("sdfdsfs"); //xxx += s+"\n"; p1.StartInfo.RedirectStandardInput = true; p1.StartInfo.RedirectStandardOutput = false; p1.StartInfo.FileName = asProcesses[1]; p1.Start(); StreamWriter sw = p1.StandardInput; sw.Write(xxx); sw.Close()

Stream Reader process cannot access file because its in use by another process [duplicate]

守給你的承諾、 提交于 2019-12-01 06:36:12
问题 This question already has answers here : How can I read a file even when getting an “in use by another process” exception? (4 answers) Closed 3 years ago . My application parses log files but when trying to parse the current day's file I get an error stating that the file is being used by another process. This log file is currently being written to and can be accessed through notepad but not through my application. Current Code: Stream stream = new FileStream(fileToRead, FileMode.Open,

How to read the Website content in c#?

浪子不回头ぞ 提交于 2019-12-01 04:13:28
I want to read the website text without html tags and headers. i just need the text displayed in the web browser. i don't need like this <html> <body> bla bla </td><td> bla bla <body> <html> i just need the text "bla bla bla bla". I have used the webclient and httpwebrequest methods to get the HTML content and to split the received data but it is not possible because if i change the website the tags may change. So is there any way to get only the displayed text in the website anagrammatically? Here is how you would do it using the HtmlAgilityPack . First your sample HTML: var html = "<html>\r

Need to pick up line terminators with StreamReader.ReadLine()

孤人 提交于 2019-11-30 19:36:52
I wrote a C# program to read an Excel .xls/.xlsx file and output to CSV and Unicode text. I wrote a separate program to remove blank records. This is accomplished by reading each line with StreamReader.ReadLine() , and then going character by character through the string and not writing the line to output if it contains all commas (for the CSV) or all tabs (for the Unicode text). The problem occurs when the Excel file contains embedded newlines (\x0A) inside the cells. I changed my XLS to CSV converter to find these new lines (since it goes cell by cell) and write them as \x0A, and normal