string fullOrRelativePath = "testfile.txt";
string fileData;
using (var reader = new StreamReader(fullOrRelativePath))
{
fileData = reader.ReadToEnd();
}
Note that this StreamReader
constructor overload does some auto encoding detection, which may or may not conform to the actual encoding used in the file.
Please note that there are some convenience methods that read all text from file available on the System.IO.File
class, namely File.ReadAllText(path)
and File.ReadAllLines(path)
.
First, let's see three different ways of extracting data from a file.
string fileText = File.ReadAllText(file);
string[] fileLines = File.ReadAllLines(file);
byte[] fileBytes = File.ReadAllBytes(file);
Next, let's see three different methods of appending data to a file. If the file you specify doesn't exist, each method will automatically create the file before attempting to append the data to it.
File.AppendAllText(file, "Here is some data that is\nappended to the file.");
File.AppendAllLines(file, new string[2] { "Here is some data that is", "appended to the file." });
using (StreamWriter stream = File.AppendText(file))
{
stream.WriteLine("Here is some data that is");
stream.Write("appended to the file.");
}
File.AppendText
to open up a streamwriter which will append whatever data is written to it.And lastly, let's see three different methods of writing data to a file. The difference between appending and writing being that writing over-writes the data in the file while appending adds to the data in the file. If the file you specify doesn't exist, each method will automatically create the file before attempting to write the data to it.
File.WriteAllText(file, "here is some data\nin this file.");
File.WriteAllLines(file, new string[2] { "here is some data", "in this file" });
File.WriteAllBytes(file, new byte[2] { 0, 255 });
using System.IO.Ports;
string[] ports = SerialPort.GetPortNames();
for (int i = 0; i < ports.Length; i++)
{
Console.WriteLine(ports[i]);
}
using System.IO.Ports;
SerialPort port = new SerialPort();
SerialPort port = new SerialPort("COM 1"); ;
SerialPort port = new SerialPort("COM 1", 9600);
NOTE: Those are just three of the seven overloads of the constructor for the SerialPort type.
The simplest way is to use the SerialPort.Read
and SerialPort.Write
methods.
However you can also retrieve a System.IO.Stream
object which you can use to stream data over the SerialPort. To do this, use SerialPort.BaseStream
.
Reading
int length = port.BytesToRead;
//Note that you can swap out a byte-array for a char-array if you prefer.
byte[] buffer = new byte[length];
port.Read(buffer, 0, length);
You can also read all data available:
string curData = port.ReadExisting();
Or simply read to the first newline encountered in the incoming data:
string line = port.ReadLine();
Writing
The easiest way to write data over the SerialPort is:
port.Write("here is some text to be sent over the serial port.");
However you can also send data over like this when needed:
//Note that you can swap out the byte-array with a char-array if you so choose.
byte[] data = new byte[1] { 255 };
port.Write(data, 0, data.Length);