Initial commit: C# 学习笔记和示例代码
- Lesson 01-10: C# 基础语法 - WebView2: 集成示例 - notes/: 详细笔记
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Lesson09
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
string basePath = Directory.GetCurrentDirectory();
|
||||
string testFile = Path.Combine(basePath, "test.txt");
|
||||
string configFile = Path.Combine(basePath, "config.json");
|
||||
string logFile = Path.Combine(basePath, "app.log");
|
||||
|
||||
Console.WriteLine("=== 文件读写演示 ===\n");
|
||||
|
||||
// ========== 写入文本文件 ==========
|
||||
Console.WriteLine("--- 写入文本文件 ---");
|
||||
string content = "第一行:无言势字幕\n第二行:VRChat 语音识别工具";
|
||||
File.WriteAllText(testFile, content);
|
||||
Console.WriteLine($"已写入: {testFile}");
|
||||
|
||||
// ========== 读取文本文件 ==========
|
||||
Console.WriteLine("\n--- 读取文本文件 ---");
|
||||
string readContent = File.ReadAllText(testFile);
|
||||
Console.WriteLine("文件内容:");
|
||||
Console.WriteLine(readContent);
|
||||
|
||||
// ========== 按行读写 ==========
|
||||
Console.WriteLine("\n--- 按行读取 ---");
|
||||
string[] lines = File.ReadAllLines(testFile);
|
||||
for (int i = 0; i < lines.Length; i++)
|
||||
{
|
||||
Console.WriteLine($"行 {i + 1}: {lines[i]}");
|
||||
}
|
||||
|
||||
// ========== JSON 配置 ==========
|
||||
Console.WriteLine("\n--- JSON 配置读写 ---");
|
||||
|
||||
// 创建设置对象
|
||||
AppSettings settings = new AppSettings
|
||||
{
|
||||
Sensitivity = 75,
|
||||
AutoConnect = true,
|
||||
VadEnabled = true,
|
||||
Language = "zh-CN"
|
||||
};
|
||||
|
||||
// 序列化为 JSON
|
||||
string json = JsonSerializer.Serialize(settings, new JsonSerializerOptions
|
||||
{
|
||||
WriteIndented = true // 格式化输出
|
||||
});
|
||||
File.WriteAllText(configFile, json);
|
||||
Console.WriteLine("已保存配置: config.json");
|
||||
Console.WriteLine(json);
|
||||
|
||||
// 反序列化
|
||||
string loadedJson = File.ReadAllText(configFile);
|
||||
AppSettings loadedSettings = JsonSerializer.Deserialize<AppSettings>(loadedJson);
|
||||
Console.WriteLine("\n读取的配置:");
|
||||
Console.WriteLine($" 灵敏度: {loadedSettings.Sensitivity}");
|
||||
Console.WriteLine($" 自动连接: {loadedSettings.AutoConnect}");
|
||||
Console.WriteLine($" VAD: {loadedSettings.VadEnabled}");
|
||||
Console.WriteLine($" 语言: {loadedSettings.Language}");
|
||||
|
||||
// ========== 追加日志 ==========
|
||||
Console.WriteLine("\n--- 追加日志 ---");
|
||||
string timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
string logEntry = $"[{timestamp}] 应用启动\n";
|
||||
File.AppendAllText(logFile, logEntry);
|
||||
Console.WriteLine($"已追加日志: {logFile}");
|
||||
|
||||
// ========== 文件操作 ==========
|
||||
Console.WriteLine("\n--- 文件操作 ---");
|
||||
Console.WriteLine($"文件是否存在: {File.Exists(testFile)}");
|
||||
Console.WriteLine($"文件大小: {new FileInfo(testFile).Length} 字节");
|
||||
Console.WriteLine($"完整路径: {Path.GetFullPath(testFile)}");
|
||||
Console.WriteLine($"扩展名: {Path.GetExtension(testFile)}");
|
||||
|
||||
// ========== 清理 ==========
|
||||
Console.WriteLine("\n--- 清理测试文件 ---");
|
||||
if (File.Exists(testFile)) File.Delete(testFile);
|
||||
if (File.Exists(configFile)) File.Delete(configFile);
|
||||
if (File.Exists(logFile)) File.Delete(logFile);
|
||||
Console.WriteLine("已删除测试文件");
|
||||
|
||||
Console.WriteLine("\n=== 演示完成 ===");
|
||||
Console.ReadLine();
|
||||
}
|
||||
}
|
||||
|
||||
// ========== 配置类 ==========
|
||||
class AppSettings
|
||||
{
|
||||
public int Sensitivity { get; set; } = 70;
|
||||
public bool AutoConnect { get; set; } = true;
|
||||
public bool VadEnabled { get; set; } = true;
|
||||
public string Language { get; set; } = "zh-CN";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user