Untitled
unknown
plain_text
2 years ago
1.4 kB
4
Indexable
using System; using System.IO; public class SingletonStreamWriter { private static readonly Lazy<SingletonStreamWriter> instance = new Lazy<SingletonStreamWriter>(() => new SingletonStreamWriter()); private StreamWriter streamWriter; // Private constructor to prevent external instantiation private SingletonStreamWriter() { // Initialize the stream writer here (replace "filePath" with your desired file path). // The "true" parameter indicates that the StreamWriter should append to the file if it exists. streamWriter = new StreamWriter("filePath", true); } // Public property to access the Singleton instance public static SingletonStreamWriter Instance => instance.Value; // Write function to write data to the stream public void Write(string data) { // Check if the stream writer has been disposed (closed) before writing if (streamWriter != null) { streamWriter.WriteLine(data); // You may also consider calling streamWriter.Flush() here if you want to flush the data immediately. } } // Close function to close the stream writer public void Close() { // Dispose the stream writer to close the file and release resources streamWriter?.Dispose(); streamWriter = null; } }
Editor is loading...