Hello
There seems to be different methods to create a text file in C#.

I am getting a byte array from USB device. I need to store this as text file which is to be read, updated, and saved for future usage.

The data comes in different packets not in a single stretch.
which is the most suitable way for this purpose.


Thank you

Roy Thomas

Edited 7 Years Ago by RoyMicro: n/a

If you intend to parse or convert the byte[] into its string equivalents, the StreamWriter should work fine as long as long as it does not need to be thread safe. For thread safety, look at TextWriter .

If you just want to write the byte[] to a file, look at FileStream class.

If you intend to parse the byte[] array into various types, consider using the BinaryWriter class.

Comments
clear and concise :)

For text file access i use StreamReader and StreamWriter objects. You can use them to write a new line for each packet of data as it is recieved:

using System;
using System.IO;

class Test 
{
    public static void Main() 
    {
        // Create an instance of StreamWriter to write text to a file.
        // The using statement also closes the StreamWriter.
        using (StreamWriter sw = new StreamWriter("TestFile.txt")) 
        {
            // Add some text to the file.
            sw.Write("This is the ");
            sw.WriteLine("header for the file.");
            sw.WriteLine("-------------------");
            // Arbitrary objects can also be written to the file.
            sw.Write("The date is: ");
            sw.WriteLine(DateTime.Now);
        }
    }
}

Code came from msdn.

Edited 7 Years Ago by Geekitygeek: n/a

As DdoubleD rightly pointed out, there are different methods that are useful for different situations, so it comes down to which tool fits the job :)

Thanks for the reply. Got an idea to start with. I think I will need to use different methods on different occasions for convenience.
btw If I want to create the file (text file) in the current folder where my exe is running, how should I pass the file name argument in filestream instantiation?
I am relatively new to C# (Windows programming in general). I am basically a Microcontroller programmer who needs some windows application development skills.
Thanks
Roy Thomas

Here is a method that uses the FileStream object, but you can also use the StreamWriter object to write data to a text file. The code below creates the file if it doesn't exist, then writes the data to the text file. If the file already exist you will use File.Append. Some of this code is from the Wrox 2005 C# book. Hope this helps.

public static void FileStreamOpenOrCreate(string path, char[] charData)
        {

            FileStream fs = new FileStream(path, FileMode.OpenOrCreate);

            byte[] byteData = new byte[charData.Length];

            Encoder e = Encoding.UTF8.GetEncoder();

            e.GetBytes(charData, 0, charData.Length, byteData, 0, true);

            fs.Seek(0, SeekOrigin.Begin);

            fs.Write(byteData, 0, byteData.Length);

            fs.Dispose();

        }

If I want to create the file (text file) in the current folder where my exe is running, how should I pass the file name argument in filestream instantiation?
Thanks
Roy Thomas

Hi Roy. If you pass in just the filename (no path), the file will be created in the same folder as where your application is running, which is the current working directory. The only caveat to this is, if your application's process changes the current working directory.

Comments
Clear explanation.

Thanks a lot. Almost clear. The text file is ok now in my application.
I have to move on to other processing once the file is full and ready.

Thanks a lot.
Roy Thomas

This question has already been answered. Start a new discussion instead.