locked
So many ways to write to a file... can someone help me figure out best practice? RRS feed

  • Question

  • I've looked online and found mulitple ways to write to a file. it's actually a little discouraging because I don't know why there are so many ways to do it, and which one is best practice?

    I've seen:

    • Streams
    • StreamWriters
    • FileWriters
    • TextWriters
    • Files
    • Filestreams

    Can someone help me figure out the best practice of writing to a text file? Which is best and what are the differences?

     And then to top that off I'm trying to learn TraceListeners, and one of those is a TextWriterTraceListener that also has 5 different ways to output to text files.

    Thursday, May 9, 2013 12:40 AM

Answers

All replies

  • For simple Text, Filestream is usually a good starting point.

    Stream is the abstract base class of all Streams (inlucding Filestream).You cannot instantiate it. But a lot of times a function that needs streams, could work with any stream.

    Wich is best depends what you actually ahve to store. CSV data? XML? Where does the text come from? How easy is it suppsoed to be parsed when reading in?


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2

    Thursday, May 9, 2013 12:57 AM
  • StreamWriter never gave me problems and It's my best but hey it depends on the things you are doing like if you are on Networking programs (Tcp, UdpClient, etc) you might consider Streams.
    Thursday, May 9, 2013 6:37 AM
  • Best Practice is, "Use what suits to the software and its requirements"

    Faisal Ahmed Farooqui —————————— If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, May 9, 2013 7:44 AM
  • public void LogMessageToFile(string msg)
            {
    
                System.IO.StreamWriter sw = System.IO.File.AppendText(
                    GetTempPath() + @"MyLogFile.txt");
                try
                {
                    string logLine = System.String.Format(
                        "{0:G}: {1}.", System.DateTime.Now, msg);
                    sw.WriteLine(logLine);
                }
                finally
                {
                    sw.Close();
                }
            }

    As Kushwaha

    Thursday, May 9, 2013 7:52 AM
    • Marked as answer by Lance Stern Wednesday, June 5, 2013 12:41 AM
    Thursday, May 9, 2013 7:52 AM
  • Hello

    Best way :

    try
    {
        if (File.Exists(fileName))
        {
            File.Delete(fileName);
        }
        using (FileStream stream = File.OpenWrite(fileName))
        using (StreamWriter writer = new StreamWriter(stream))
        {
            writer.Write("your content");
        }        
    }
    catch (IOException)
    {
        // the file is locked
    }


    As Kushwaha

    Thursday, May 9, 2013 7:53 AM
  • Hi,

    The general idea is to expose data (not evne just file data) as classes inheriting from a base Stream class. Then which stream you use depends if you prefer to see your data as a series of bytes, or as encoded text and so on.

    You also have some tolls to quickly do something (for example if you need to write a single line of text to a file, you can do so with a single method call).

    There is not best way. See first what you want to do and then depending on the kind of data you'll choose among those streams...


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".

    Thursday, May 9, 2013 12:29 PM