[英]How to write to a file in .NET Core?
我想使用 .NET Core(特別是 BluetoothLEAdvertisementWatcher)中的藍牙 LE 功能來編寫一個將信息記錄到文件中的掃描器。 這是作為桌面應用程序運行,最好作為命令行應用程序運行。
顯然,像 System.IO.StreamWriter(string) 這樣的構造函數不可用。 如何創建文件並寫入文件?
我很高興能夠執行 System.Console.WriteLine(string) ,但這似乎在 .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!");
}
}
}
更新 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": {}
}
}
命令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?)
...
這段代碼是我提出問題時一直在尋找的骨架。 它僅使用 .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();
這是我正在使用的解決方案。 它使用更少的代碼行並且同樣出色地完成工作。 它也非常兼容 .NET core 2.0
using (StreamWriter writer = System.IO.File.AppendText("logfile.txt"))
{
writer.WriteLine("log message");
}
更好的是:
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
}
如果您喜歡編寫更少的代碼行,則可以將上面的代碼重寫為
using (var writer = System.IO.File.CreateText(System.IO.Path.GetTempFileName()))
{
writer.WriteLine("log message"); //or .Write(), if you wish
}
到今天為止,使用 RTM,似乎也有這種更短的方法:
var watcher = new BluetoothLEAdvertisementWatcher();
var logPath = System.IO.Path.GetTempFileName();
var logWriter = System.IO.File.CreateText(logPath);
logWriter.WriteLine("Log message");
logWriter.Dispose();
您可以使用corefx git 存儲庫獲取 System.IO 引用。 這將使您正在尋找的 StreamWrite(string) 構造函數可用。
此存儲庫還將為您提供您正在尋找的 Console.WriteLine(string) 函數。
這是一個異步 FileWriter 類。
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);
}
}
}
要在 .NET Core 中寫入文件,可以使用兩種方法:
AppendText()
CreateText()
這兩個方法是System.IO
命名空間中File
類的靜態成員。 它們之間的區別在於一個附加到現有文件,而另一個覆蓋該文件。
使用示例:
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");
}
讓它更簡單:
using System.IO;
----------------
var watcher = new BluetoothLEAdvertisementWatcher();
var streamWriter = new StreamWriter($"Enter\\filepath\\here\\Log.txt");
streamWriter.WriteLine("Log Message");
streamWriter.Dispose();
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.