C# 读取本地 JSON 文件:使用 System.IO.File 类
使用 C# 的 System.IO 命名空间下的 File 类可以轻松读取本地 JSON 文件。以下是一个示例代码:
using System;
using System.IO;
class Program
{
static void Main()
{
string path = 'path/to/your/json/file.json';
if (File.Exists(path))
{
string json = File.ReadAllText(path);
Console.WriteLine(json);
}
else
{
Console.WriteLine('File not found.');
}
}
}
在上述代码中,将 path/to/your/json/file.json 替换为实际 JSON 文件的路径。然后使用 File.Exists 方法检查文件是否存在,如果存在,则使用 File.ReadAllText 方法读取文件内容,并将其打印到控制台。如果文件不存在,则打印 'File not found.'。
原文地址: https://www.cveoy.top/t/topic/pkoK 著作权归作者所有。请勿转载和采集!