Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write to a file in .NET Core?

I want to use the Bluetooth LE functions in .NET Core (specifically, BluetoothLEAdvertisementWatcher) to write a scanner which logs information to a file. This is to run as a desktop application and preferably as a command line app.

Constructors like System.IO.StreamWriter(string) are not available, apparently. How do I create a file and write to it?

I would be just as happy to be able to do a System.Console.WriteLine(string) but that doesn't seem to be available under .NET Core either.

Update: To clarify, if I could have a program that looks like this run without error, I'll be off to the races.

using System;
using Windows.Devices.Bluetooth.Advertisement;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BluetoothLEAdvertisementWatcher watcher = new BluetoothLEAdvertisementWatcher();
            Console.WriteLine("Hello, world!");
        }
    }
}

Update 2: Here's the project.json file:

{
  "dependencies": {
    "Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0"
  },
  "frameworks": {
    "uap10.0": {}
  },
  "runtimes": {
    "win10-arm": {},
    "win10-arm-aot": {},
    "win10-x86": {},
    "win10-x86-aot": {},
    "win10-x64": {},
    "win10-x64-aot": {}
  }
}

The output of the command dotnet -v run contains this error message:

W:\src\dotnet_helloworld>dotnet -v run
...
W:\src\dotnet_helloworld\Program.cs(2,15): error CS0234: The type or namespace name 'Devices' does not exist in the namespace 'Windows' (are you missing an assembly reference?)
...
like image 609
gauss256 Avatar asked Feb 10 '16 08:02

gauss256


People also ask

How do I write to an existing text file in C#?

AppendText() Method in C# with Examples. File. AppendText() is an inbuilt File class method which is used to create a StreamWriter that appends UTF-8 encoded text to an existing file else it creates a new file if the specified file does not exist.

How do I write to a text file?

Steps for writing to text files First, open the text file for writing (or append) using the open() function. Second, write to the text file using the write() or writelines() method. Third, close the file using the close() method.

Where does StreamWriter write to?

StreamWriter. WriteLine() method writes a string to the next line to the steam. The following code snippet creates and writes different author names to the stream.


4 Answers

This code is the skeleton I was looking for when I posed the question. It uses only facilities available in .NET Core.

var watcher = new BluetoothLEAdvertisementWatcher();

var logPath = System.IO.Path.GetTempFileName();
var logFile = System.IO.File.Create(logPath);
var logWriter = new System.IO.StreamWriter(logFile);
logWriter.WriteLine("Log message");
logWriter.Dispose();
like image 114
gauss256 Avatar answered Oct 20 '22 02:10

gauss256


This is the solution I'm using. It uses fewer lines of code and does the job just as good. It's also very compatible with .NET core 2.0

using (StreamWriter writer = System.IO.File.AppendText("logfile.txt"))
{
    writer.WriteLine("log message");
}
like image 25
Temi Lajumoke Avatar answered Oct 20 '22 04:10

Temi Lajumoke


Even better:

using System.IO;

var logPath = Path.GetTempFileName();
using (var writer = File.CreateText(logPath)) // or File.AppendText
{
    writer.WriteLine("log message"); //or .Write(), if you wish
}

If writing fewer lines of code is your thing, the above can be re-written as

using (var writer = System.IO.File.CreateText(System.IO.Path.GetTempFileName()))
{
    writer.WriteLine("log message"); //or .Write(), if you wish
}
like image 25
Soma Mbadiwe Avatar answered Oct 20 '22 03:10

Soma Mbadiwe


As of today, with RTM, there seems to be this shorter way as well:

var watcher = new BluetoothLEAdvertisementWatcher();

var logPath = System.IO.Path.GetTempFileName();
var logWriter = System.IO.File.CreateText(logPath);
logWriter.WriteLine("Log message");
logWriter.Dispose();
like image 10
superjos Avatar answered Oct 20 '22 03:10

superjos