txt texto sobrescribir net manejo lineas leer guardar escribir ejemplos crear archivos archivo agregar c# .net bluetooth coreclr

texto - sobrescribir archivo c#



¿Cómo escribir en un archivo en.NET Core? (5)

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

Los constructores como System.IO.StreamWriter (cadena) no están disponibles, al parecer. ¿Cómo creo un archivo y escribo en él?

Sería tan feliz de poder hacer un System.Console.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 sin errores, me iré a 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 archivo project.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?) ...


A partir de hoy, con RTM, parece haber esta manera más corta 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();


Aun mejor:

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


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

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


Este código es el esqueleto que estaba buscando cuando hice 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();


Puede obtener las referencias de System.IO utilizando el repositorio corefx git. Esto hará que el constructor StreamWrite (cadena) que está buscando esté disponible.

Este repositorio también le dará la función Console.WriteLine (cadena) que está buscando.