53

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?)
...
gauss256
  • 2,314
  • 3
  • 19
  • 22
  • Is it the same as you need to do for WinRT? http://blog.jerrynixon.com/2012/06/windows-8-how-to-read-files-in-winrt.html – Matthew Watson Feb 10 '16 at 08:31
  • Can you share the project.json file? You might be missing some package references. – Victor Hurdugaci Feb 12 '16 at 02:19
  • @VictorHurdugaci I've edited the question to include one of the project.json files I've tried, along with the error message that results. No doubt the references are wrong, I'm kind of flying blind here. This particular project.json was taken from a Visual Studio 2015 project template for a .NET Core project. – gauss256 Feb 12 '16 at 06:20

8 Answers8

94

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();
gauss256
  • 2,314
  • 3
  • 19
  • 22
  • 4
    Works great. I'm guessing in about six months this will have several hundred upvotes. – GDB Jul 24 '16 at 13:04
25

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");
}
Temi Lajumoke
  • 1,930
  • 1
  • 12
  • 14
22

Even better:

using System.IO;

var logPath = Path.GetTempFileName();
using (var writer = File.CreateText(logPath))
{
    writer.WriteLine("log message"); //or .Write(), if you wish
}
Dekim
  • 534
  • 6
  • 19
Soma Mbadiwe
  • 1,331
  • 13
  • 13
10

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();
superjos
  • 10,834
  • 4
  • 79
  • 120
1

You can get the System.IO references by using the corefx git repository. This will make the StreamWrite(string) constructor you are looking for available.

This repository will also give you the Console.WriteLine(string) function you are looking for.

Fanus du Toit
  • 926
  • 1
  • 10
  • 26
  • Sounds good, but I'm still stuck. I've installed and built CoreFX. Now what? Do I add it as a reference in a Visual Studio project? Do I prep and run my program using `dotnet` commands? I've added a code snippet to my original question to clarify what I'd like to accomplish as a first step. – gauss256 Feb 11 '16 at 00:00
  • Once it is built you need to add it as a reference yes, then you will be able to include the System.IO library by putting in a using at the top of the file `using System.IO;` – Fanus du Toit Feb 11 '16 at 08:09
  • There may be a way to do this, but every attempt I made to include references to CoreFX conflicted with .NET Core. – gauss256 Feb 15 '16 at 03:17
1

Here is an async FileWriter class.

using System.IO;
public static class AsyncFileWriter
{
    public static async Task WriteToFile(string content)
    {
        var logPath = @"SOME_PATH\log.txt";
        using (var writer = File.CreateText(logPath))
        {
            await writer.WriteLineAsync(content); 
        }
    }
}
Vishav Premlall
  • 376
  • 4
  • 19
0

To write to files in .NET Core, you can use two methods:

  1. AppendText()
  2. CreateText()

These two methods are static members of the File class in the System.IO namespace. The difference between them is one appends to an existing file while the other overwrites the file.

Usage examples:

AppendText

using (StreamWriter writer = System.IO.File.AppendText("file.txt"))
{
    writer.WriteLine("message");
}

CreateText

using (StreamWriter writer = System.IO.File.CreateText("file.txt"))
{
    writer.WriteLine("message");
}
codejockie
  • 5,692
  • 3
  • 29
  • 36
-1

Made it more simple:

    using System.IO;
    ----------------

    var watcher = new BluetoothLEAdvertisementWatcher();

    var streamWriter = new StreamWriter($"Enter\\filepath\\here\\Log.txt");

    streamWriter.WriteLine("Log Message");

    streamWriter.Dispose();

Hope it helps!

Arjun MG
  • 81
  • 8