streamwriter

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

using StreamWriter on server in C#

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 10:49:13
问题 I try to write to a remote machine with the following code: StreamWriter(@"\\" + remoteMachine + "\\admin$\\" + fileName); i get the following error Logon failure: unknown user name or bad password. i got the user name, domain and password. how can i write with credentials? if i already been to that computer in this session there is no problem, but if it's a new session the computer doesn't recognize the location how can i write with credentials? 回答1: You'll need to impersonate the identity

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

StreamWriter: (Write+Flush)Async seem much slower then synchronous variants

∥☆過路亽.° 提交于 2019-12-01 01:04:27
I was trying to find an IO bottleneck in my class and surprisingly noticed that sw.Write("m"); sw.Flush() can be 20000 faster then await sw.WriteAsync("m"); Await sw.FlushAsync(); when writing 1000000 messages to a file. Does, by any chance, anyone know why? My bet is StreamWriter 's constructor taking a String does not parametrize a stream for async usage. The code below can be launched from C# interactive. Yes, it's not the best place to measure the speed but it will show the matter at hand anyway: var sr = new StreamWriter("G:\\file.file"); var N = 1000; var sw = new Stopwatch(); sw.Start()

Using StreamWriter to implement a rolling log, and deleting from top

三世轮回 提交于 2019-11-30 19:28:20
My C# winforms 4.0 application has been using a thread-safe streamwriter to do internal, debug logging information. When my app opens, it deletes the file, and recreates it. When the app closes, it saves the file. What I'd like to do is modify my application so that it does appending instead of replacing. This is a simple fix. However, here's my question: I'd like to keep my log file AROUND 10 megabytes maximum. My constraint would be simple. When you go to close the file, if the file is greater than 10 megabytes, trim out the first 10%. Is there a 'better' way then doing the following: Close

How to make a “Read only” file?

你。 提交于 2019-11-30 18:59:30
I'm using the C# StreamWritier class. Questions: How can I make a file read-only, so that no one can delete or write to it? How can I make a hidden file? I'm creating the file like so: private void button1_Click(object sender, EventArgs e) { SaveFileDialog save = new SaveFileDialog(); save.FileName = textBox1.Text; save.Filter = "Text File | *.rtf"; if (save.ShowDialog() == DialogResult.OK) { StreamWriter writer = new StreamWriter(save.OpenFile()); writer.WriteLine(textBox2.Text); } writer.Dispose(); writer.Close(); } Aghilas Yakoub Hello you can try with this method 1 public static void

c# HttpWebRequest POST'ing failing

青春壹個敷衍的年華 提交于 2019-11-30 13:01:38
So i'm trying to POST something to a webserver. System.Net.HttpWebRequest EventReq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("url"); System.String Content = "id=" + Id; EventReq.ContentLength = System.Text.Encoding.UTF8.GetByteCount(Content); EventReq.Method = "POST"; EventReq.ContentType = "application/x-www-form-urlencoded"; System.IO.StreamWriter sw = new System.IO.StreamWriter(EventReq.GetRequestStream(), System.Text.Encoding.UTF8); sw.Write(Content); sw.Flush(); sw.Close(); Looks alright, i'm setting content-length based on the size of the ENCODED data... Anyway it

What is the purpose of StreamReader when Stream.Read() exists?

做~自己de王妃 提交于 2019-11-30 07:27:21
问题 This has been bugging me. I know Stream is an abstract class and therefore can't be instantiated but it has classes that are derived from it. Why is there a StreamReader class and a Stream.Read() method (and vice verse for StreamWriter and Stream.Write() )? You can write to a text file using 3 million different methods and it's rather frustrating trying to get my head around all of these different types and methods in the System.IO namespace. I found questions and answers regarding the

Try/Finally block vs calling dispose?

放肆的年华 提交于 2019-11-30 07:18:15
Is there any difference between these two code samples and if not, why does using exist? StreamWriter writer; try { writer = new StreamWriter(...) writer.blahblah(); } finally { writer.Dispose(); } vs: using (Streamwriter writer = new Streamwriter(...)) { writer.blahblah } I mean in the second example you really should be putting it in a try block anyway so adding the finally block really doesn't use much more effort. I get that the entire thing might be encompassed in a bigger try block but yeah, just seems superfluous to me. There're some issues with your code. Never write like this (put

Using StreamWriter to implement a rolling log, and deleting from top

岁酱吖の 提交于 2019-11-30 03:38:43
问题 My C# winforms 4.0 application has been using a thread-safe streamwriter to do internal, debug logging information. When my app opens, it deletes the file, and recreates it. When the app closes, it saves the file. What I'd like to do is modify my application so that it does appending instead of replacing. This is a simple fix. However, here's my question: I'd like to keep my log file AROUND 10 megabytes maximum. My constraint would be simple. When you go to close the file, if the file is