C# 如何在.NET Core中写入文件?
我想使用.NET Core(特别是BluetoothLEAdvertisementWatcher)中的Bluetooth LE函数来编写一个扫描仪,将信息记录到文件中。这将作为桌面应用程序运行,最好是作为命令行应用程序运行 像System.IO.StreamWriter(string)这样的构造函数显然不可用。如何创建文件并写入 我也很高兴能够使用System.Console.WriteLine(字符串),但在.NETCore下似乎也不可用 更新:为了澄清,如果我能有一个程序,看起来像这个运行没有错误,我会去比赛C# 如何在.NET Core中写入文件?,c#,.net,bluetooth,coreclr,C#,.net,Bluetooth,Coreclr,我想使用.NET Core(特别是BluetoothLEAdvertisementWatcher)中的Bluetooth LE函数来编写一个扫描仪,将信息记录到文件中。这将作为桌面应用程序运行,最好是作为命令行应用程序运行 像System.IO.StreamWriter(string)这样的构造函数显然不可用。如何创建文件并写入 我也很高兴能够使用System.Console.WriteLine(字符串),但在.NETCore下似乎也不可用 更新:为了澄清,如果我能有一个程序,看起来像这个运行没
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?)
...
您可以使用git存储库获取System.IO引用。这将使您正在寻找的StreamWrite(字符串)构造函数可用
此存储库还将为您提供正在查找的Console.WriteLine(string)函数。此代码是我提出问题时正在查找的框架。它只使用.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();
到今天为止,RTM似乎也有这种较短的方法:
var-watcher=new BluetoothLEAdvertisementWatcher();
var logPath=System.IO.Path.GetTempFileName();
var logWriter=System.IO.File.CreateText(logPath);
logWriter.WriteLine(“日志消息”);
logWriter.Dispose();
更好的是:
using System.IO;
var logPath = Path.GetTempFileName();
using (var writer = File.CreateText(logPath))
{
writer.WriteLine("log message"); //or .Write(), if you wish
}
这就是我正在使用的解决方案。它使用更少的代码行,并且做得同样好。它还与.NETCore2.0非常兼容
using (StreamWriter writer = System.IO.File.AppendText("logfile.txt"))
{
writer.WriteLine("log message");
}
让它变得更简单:
using System.IO;
----------------
var watcher = new BluetoothLEAdvertisementWatcher();
var streamWriter = new StreamWriter($"Enter\\filepath\\here\\Log.txt");
streamWriter.WriteLine("Log Message");
streamWriter.Dispose();
希望有帮助 这里是一个异步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
命名空间中文件
类的静态成员。它们之间的区别是一个附加到现有文件,而另一个覆盖该文件
用法示例:
AppendText
using (StreamWriter writer = System.IO.File.AppendText("file.txt"))
{
writer.WriteLine("message");
}
using (StreamWriter writer = System.IO.File.CreateText("file.txt"))
{
writer.WriteLine("message");
}
CreateText
using (StreamWriter writer = System.IO.File.AppendText("file.txt"))
{
writer.WriteLine("message");
}
using (StreamWriter writer = System.IO.File.CreateText("file.txt"))
{
writer.WriteLine("message");
}
这和你需要为WinRT做的一样吗?您可以共享project.json文件吗?您可能缺少一些包引用。@VictorHurdugaci我已编辑了该问题,以包含我尝试过的一个project.json文件,以及由此产生的错误消息。毫无疑问,参考文献是错误的,我在这里有点盲目。这个特殊的project.json是从一个.NET核心项目的Visual Studio 2015项目模板中获取的。听起来不错,但我仍然被卡住了。我已经安装并构建了CoreFX。现在怎么办?我是否将其添加为Visual Studio项目中的引用?我是否使用
dotnet
命令准备并运行程序?我在我的原始问题中添加了一个代码片段,以澄清作为第一步我想要完成的任务。一旦构建完成,您需要将其添加为参考。是的,那么您将能够通过在文件using System.IO;的顶部添加using来包括System.IO库代码>可能有办法做到这一点,但我每次尝试包含对CoreFX的引用都会与.NETCore.Works产生冲突。我猜大约六个月后,这将有几百张投票。如果您想替换文本,请使用System.IO.File.CreateText
而不是System.IO.File.AppendText