streamwriter

How to detect a Socket Disconnect in C#

删除回忆录丶 提交于 2019-12-13 11:40:11
问题 I'm working on a client/server relationship that is meant to push data back and forth for an indeterminate amount of time. The problem I'm attempting to overcome is on the client side, being that I cannot manage to find a way to detect a disconnect. I've taken a couple of passes at other peoples solutions, ranging from just catching IO Exceptions, to polling the socket on all three SelectModes. I've also tried using a combination of a poll, with a check on the 'Available' field of the socket.

streamread can't read any data from NetworkStream in c# ,

柔情痞子 提交于 2019-12-13 08:29:00
问题 I have a sensor that has an ip and port 192.168.2.44:3000. I used the herculas to connect to the device ,as you can see in the picture : enter image description here I need to implement this software in c# ,so i write this code : private static void Main(string[] args) { try { byte[] buffer = new byte[2048]; // read in chunks of 2KB int bytesRead; var listener = new TcpListener(IPAddress.Any, 3000); listener.Start(); NetworkStream network_stream; StreamReader read_stream; StreamWriter write

How to enable seeking in Azure Blob stream

做~自己de王妃 提交于 2019-12-13 06:26:15
问题 I have a BlobStream that is created from the method OpenWriter. var blob = CloudContainer.GetBlobReference(name)); if (blob == null) { return null; } return blob.OpenWrite(); Using this stream i would like to seek or set the position, but each time i do this i get a NotSupportedException. After doing some research i found that the canSeek is set to false, which causes this problem. But, the CanSeek is false only if the length is unknown. But the length is known when i run the debugger. Why is

Overwrite file, but only parts that are not spaces

a 夏天 提交于 2019-12-13 06:23:45
问题 I am looking for a way to overwrite certain parts in a .dat file with 00000. For this I have a StringBuilder with content like this: "00000000 0000000000000000 " Now I am looking for a method that overwrites the parts in the file with zeroes, and justs keeps the content of the parts with spaces. So if I have "AUEUIGHEVjerhvgm,eiouhegfwoedjkjjjjjjjjjjjjjjje" I want it to turn into "00000000Vjerhvgm,eio0000000000000000jjjjjjjjjje" Does such a method exist? Or should I accomplish this task in

The process cannot access the file because it is being used by another process

…衆ロ難τιáo~ 提交于 2019-12-13 02:13:45
问题 I am trying to do the following: var path = Server.MapPath("File.js")); // Create the file if it doesn't exist or if the application has been restarted // and the file was created before the application restarted if (!File.Exists(path) || ApplicationStartTime > File.GetLastWriteTimeUtc(path)) { var script = "..."; using (var sw = File.CreateText(path)) { sw.Write(script); } } However occasionally the following error is sometimes thrown: The process cannot access the file '...\File.js' because

File sharing violation occurs after creation of file?

家住魔仙堡 提交于 2019-12-13 00:13:46
问题 So, I'm attempting to create a .txt file, and then write some silly data to it. But I'm getting a sharing violation. I sense that it may be because I'm attempting to create a StreamWriter for a file directly after creating it, but that doesn't make sense. So I'm a bit lost. I've tried removing all the other StreamWriters and Readers in the class except for the erroneous line, and I'm still getting a violation. The error I'm getting, IOException: Sharing violation on path C:\Users\USER\Desktop

StreamWriter won't flush to NetworkStream

时光总嘲笑我的痴心妄想 提交于 2019-12-12 21:20:49
问题 Using a StreamWriter to write to a NetworkStream , and a StreamReader to read the response. The app is sending commands and reading responses to a news server. Simplified code (sans error handling, etc.): tcpClient = new TcpClient(); tcpClient.Connect(Name, Port); networkStream = tcpClient.GetStream(); serverReader = new StreamReader(networkStream, Encoding.Default); serverWriter = new StreamWriter(networkStream, Encoding.ASCII) { AutoFlush = true }; // reads the server's response to the

Inheriting from StreamWriter with smallest possible effort

女生的网名这么多〃 提交于 2019-12-12 11:26:02
问题 I am using an external library which allows logging using StreamWriter - now I want to add some handling based on the content of the logging. As I want to avoid to loop through the log file, I would like to write a class which inherits from StreamWriter. What is the best way to inherit from StreamWriter with as few re-implementations of methods/constructors? 回答1: I'm not sure of what you want to do exactly, but if you only want to inspect what is being written in the stream, you can do this:

Sending a multiline string over NamedPipe?

岁酱吖の 提交于 2019-12-12 02:23:24
问题 How can I send a multiline string with blank lines over a NamedPipe? If I send a string string text= @"line 1 line2 line four "; StreamWriter sw = new StreamWriter(client); sw.Write(text); I get on the server side only "line 1" : StreamReader sr = new StreamReader(server); string message = sr.ReadLine(); When I try something like this StringBuilder message = new StringBuilder(); string line; while ((line = sr.ReadLine()) != null) { message.Append(line + Environment.NewLine); } It hangs in the

File encoding doesn't work

梦想的初衷 提交于 2019-12-11 08:48:42
问题 In my code string[] Lines = reactor.GetMergedLines(); string fileName = "foo.bar"; StreamWriter sw = new StreamWriter(File.Open(fileName, FileMode.CreateNew), Encoding.GetEncoding(28605)); foreach (string line in Lines) { sw.WriteLine(line); } sw.Close(); the file, which gets created is not encoded with the given codepage. Lines is filled with strings out of an iso-8859-1-file. I tried it with the code page number Encoding.GetEncoding(28605) , it's name Encoding.GetEncoding("ISO-8859-15") and