streamwriter

Self-closing StreamWriter singleton

那年仲夏 提交于 2019-12-02 04:08:29
I tried to further narrow down the problem in Flush StreamWriter at the end of its lifetime implementing a singleton-like self-closing StreamWriter : class Foo : System.IO.StreamWriter { private static readonly Foo instance = new Foo( "D:/tmp/test" ); private Foo( string path ) : base( path ) { } ~Foo() { this.Close(); } public static Foo Instance { get { return instance; } } } The intended effect is that in a sample program like class Program { private static void Main( string[] args ) { Foo.Instance.Write( "asdf\n" ); } } the garbage collector causes the instance of Foo to be closed. This

Creating text file in C#

放肆的年华 提交于 2019-12-02 00:55:24
I'm learning how to create text file in C# but I have a problem. I used this code: private void btnCreate_Click(object sender, EventArgs e) { string path = @"C:\CSharpTestFolder\Test.txt"; if (!File.Exists(path)) { File.Create(path); using (StreamWriter sw = File.CreateText(path)) { sw.WriteLine("The first line!"); } } else if (File.Exists(path)) MessageBox.Show("File with this path already exists.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); } When I press the "Create" button, Visual Studio shows an error 'System.IO.DirectoryNotFoundException', which points at "File.Create(path)".

StreamWriter automatically flushing buffer when given large messages

守給你的承諾、 提交于 2019-12-02 00:49:03
Overview I have a Client-Server whereby I have the streamWriter.WriteLine(messageToClient); on the server sending large messages. The streamReader.ReadLine(); on the client is reading the 2000 length messages fine. The issue here is the streamWriter.Flush() has no affect on the larger messages, because what I am aiming for is the server to be able to switch between the buffers flushing directly (having a fluid server message output) and not (the server response would be jumbled together until it reaches the buffers limit to flush). Question: I am thinking that the buffers are auto flushing

What happens if StreamReader or StreamWriter are not closed?

亡梦爱人 提交于 2019-12-01 19:45:29
I'm working on an assignment for a professor that is strict about LOC . For this reason I'd like to do the following: (new StreamWriter(saveFileDialog.FileName)).Write(textBox.Text); instead of StreamWriter sw = new StreamWriter(saveFileDialog.FileName); sw.Write(textBox.Text); sw.Close(); In the first example I don't close the stream. Is this ok? Will it cause any security or memory problems? You may not get any output, or incomplete output. Closing the writer also flushes it. Rather than manually calling Close at all, I'd use a using statement... but if you're just trying to write text to a

StreamWriter add an extra \\r in the end of the line

安稳与你 提交于 2019-12-01 17:58:40
I created a class with the responsibility to generate a text file where each line represents the information of an object of 'MyDataClass' class. Below is a simplification of my code: public class Generator { private readonly Stream _stream; private readonly StreamWriter _streamWriter; private readonly List<MyDataClass> _items; public Generator(Stream stream) { _stream = stream; _streamWriter = new StreamWriter(_stream, Encoding.GetEncoding("ISO-8859-1")); } public void Generate() { foreach (var item in _items) { var line = AnotherClass.GetLineFrom(item); _streamWriter.WriteLine(line); }

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

What is the fastest way to export dataGridView rows to Excel or into an SQL Server database

一笑奈何 提交于 2019-12-01 16:47:42
What is the fastest way to export DataGridView rows in the range of 460328 - 800328 to Excel or into an SQL Server database table with out using Microsoft office interop as interop is quite slow and heavy on system resources? For exporting to Excel, if you aren't using the XML based 2007 or 2010, Interop is pretty much the only way to go. It's not as bad as it's reputation though. I'll list a few solutions. 1 To Excel First add a Microsoft.Office.Interop.Excel component reference to your project. This should be under the .NET tab in Project -> Add Reference. add the using statement to your

StreamWriter add an extra \r in the end of the line

倾然丶 夕夏残阳落幕 提交于 2019-12-01 16:14:08
问题 I created a class with the responsibility to generate a text file where each line represents the information of an object of 'MyDataClass' class. Below is a simplification of my code: public class Generator { private readonly Stream _stream; private readonly StreamWriter _streamWriter; private readonly List<MyDataClass> _items; public Generator(Stream stream) { _stream = stream; _streamWriter = new StreamWriter(_stream, Encoding.GetEncoding("ISO-8859-1")); } public void Generate() { foreach

Streamwriter is cutting off my last couple of lines sometimes in the middle of a line?

不羁的心 提交于 2019-12-01 14:05:28
问题 Here is my code. : FileStream fileStreamRead = new FileStream(pathAndFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None); FileStream fileStreamWrite = new FileStream(reProcessedFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); StreamWriter sw = new StreamWriter(fileStreamWrite); int readIndex = 0; using (StreamReader sr = new StreamReader(fileStreamRead)) { while (!sr.EndOfStream) { Console.WriteLine("eof" + sr.EndOfStream); readIndex++; Console.WriteLine(readIndex

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);