Cómo escribir en un archivo in.NET ¿Núcleo?


Quiero usar las funciones Bluetooth LE en.NET Core (específicamente, BluetoothLEAdvertisementWatcher) para escribir un escáner que registre información en un archivo. Esto es para ejecutarse como una aplicación de escritorio y preferiblemente como una aplicación de línea de comandos.

Los constructores como System.IO.StreamWriter(string) no están disponibles, aparentemente. ¿Cómo puedo crear un archivo y escribir en él?

Estaría igual de feliz de poder hacer un Sistema.Consola.WriteLine (cadena) pero eso no parece estar disponible bajo. NET Core tampoco.

Actualización: Para aclarar, si pudiera tener un programa que se parece a este ejecutar sin error, voy a estar fuera de las carreras.

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!");
        }
    }
}

Actualización 2: Aquí está el proyecto.archivo json:

{
  "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": {}
  }
}

La salida del comando dotnet -v run contiene este mensaje de error:

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?)
...
Author: gauss256, 2016-02-10

5 answers

Este código es el esqueleto que estaba buscando cuando planteé la pregunta. Utiliza solo las instalaciones disponibles en. 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();
 59
Author: gauss256,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-15 03:15:00

Aún mejor:

var logPath = System.IO.Path.GetTempFileName();
using (var writer = File.CreateText(logPath))
{
    writer.WriteLine("log message"); //or .Write(), if you wish
}
 9
Author: Soma Mbadiwe,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-12 13:54:18

A partir de hoy, con RTM, parece haber este camino más corto también:

var watcher = new BluetoothLEAdvertisementWatcher();

var logPath = System.IO.Path.GetTempFileName();
var logWriter = System.IO.File.CreateText(logPath);
logWriter.WriteLine("Log message");
logWriter.Dispose();
 7
Author: superjos,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-31 11:32:42

Esta es la solución que estoy usando. Utiliza menos líneas de código y hace el trabajo igual de bien. También es muy compatible con. NET core 2.0

using (StreamWriter writer = System.IO.File.AppendText("logfile.txt"))
{
    writer.WriteLine("log message");
}
 3
Author: Temi Lajumoke,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-04-29 10:50:30

Puede obtener el System.IO referencias usando el repositorio git corefx. Esto hará que el constructor StreamWrite(string) que estás buscando esté disponible.

Este repositorio también le dará la Consola.Función WriteLine (string) que está buscando.

 1
Author: Fanus du Toit,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-10 08:35:40