gau*_*256 42 .net c# bluetooth coreclr
我想使用.NET Core中的蓝牙LE功能(特别是BluetoothLEAdvertisementWatcher)来编写将信息记录到文件的扫描程序.这是作为桌面应用程序运行,最好是作为命令行应用程序运行.
System.IO.StreamWriter(string)这样的构造函数显然不可用.如何创建文件并写入文件?
我会很高兴能够做一个System.Console.WriteLine(字符串),但似乎在.NET Core下也没有.
更新:为了澄清,如果我有一个看起来像这样运行的程序没有错误,我将参加比赛.
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!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新2:这是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": {}
}
}
Run Code Online (Sandbox Code Playgroud)
该命令的输出dotnet -v run包含此错误消息:
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?)
...
Run Code Online (Sandbox Code Playgroud)
gau*_*256 83
当我提出问题时,这段代码就是我正在寻找的骨架.它仅使用.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();
Run Code Online (Sandbox Code Playgroud)
Som*_*iwe 16
更好的是:
var logPath = System.IO.Path.GetTempFileName();
using (var writer = File.CreateText(logPath))
{
writer.WriteLine("log message"); //or .Write(), if you wish
}
Run Code Online (Sandbox Code Playgroud)
Tem*_*oke 14
这是我正在使用的解决方案.它使用较少的代码行,并且工作也同样好.它也与.NET核心2.0非常兼容
using (StreamWriter writer = System.IO.File.AppendText("logfile.txt"))
{
writer.WriteLine("log message");
}
Run Code Online (Sandbox Code Playgroud)
截至今天,使用RTM,似乎还有这种更短的方式:
var watcher = new BluetoothLEAdvertisementWatcher();
var logPath = System.IO.Path.GetTempFileName();
var logWriter = System.IO.File.CreateText(logPath);
logWriter.WriteLine("Log message");
logWriter.Dispose();
Run Code Online (Sandbox Code Playgroud)