C#开发环境搭建
2026-09-18
安装 .NET SDK
.NET SDK(软件开发工具包)包含了编译器、运行时和各种开发工具,是 C# 开发的基础。安装步骤如下:
- 访问 .NET 官方下载页面:https://dotnet.microsoft.com/download
- 选择最新的 LTS(长期支持)版本,推荐初学者优先选择 LTS 版,稳定且资料丰富。
- 根据你的操作系统(Windows / macOS / Linux)下载对应的安装包,按提示完成安装。
安装 VSCode
vscode是一个轻量级的代码编辑器,它提供了丰富的插件生态系统和强大的调试功能。通过安装相关插件,vscode可以成为一个功能强大的集成开发环境(IDE),支持多种编程语言和开发工具。
- 下载地址:https://code.visualstudio.com/Download
- 直接默认安装即可,安装中选项选择
Add to PATH,这样就不需要手动添加环境变量。
需要安装的插件,左侧栏点击扩展,搜索安装以下插件:
C# Dev Kit:提供C#语言的语法高亮、智能提示和调试功能。
创建 C# 项目
C# Demo
.vscode
launch.json
tasks.json
Demo.csproj
Program.cs
.vscode/launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net10.0/Demo.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
},
]
}.vscode/tasks.json
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"detail": "增量编译:只编译有改动的文件,供日常开发和 F5 调试",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/Demo.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"group": {
"kind": "build",
"isDefault": true
},
"problemMatcher": "$msCompile"
},
{
"label": "clean",
"detail": "清除 bin/obj 等编译输出,解决陈旧程序集或奇怪编译错误",
"command": "dotnet",
"type": "process",
"args": [
"clean",
"${workspaceFolder}/Demo.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "rebuild",
"detail": "先清除再完整编译,等价于 Visual Studio 的重新生成",
"dependsOrder": "sequence",
"dependsOn": [
"clean",
"build"
],
"group": "build",
"problemMatcher": []
},
{
"label": "publish",
"detail": "发布可部署产物(含依赖),用于拷贝到其他机器或发布目录",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/Demo.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary;ForceNoAlign"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"detail": "监视源码变化并重启进程(关闭热重载,控制台 Main 改完才能重新打印)",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"--no-hot-reload", // 关闭热重载,控制台 Main 改完才能重新打印
"run",
"--project",
"${workspaceFolder}/Demo.csproj"
],
"isBackground": true,
"presentation": {
"reveal": "always",
"panel": "dedicated"
},
"problemMatcher": "$msCompile"
}
]
}Demo.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>Program.cs
using System;
namespace DemoApp
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World");
Console.ReadKey();
}
}
}常用库
- CommunityToolkit.Mvvm:Mvvm框架,提供了ViewModel和View的分离,使得代码更加清晰易懂。
- TouchSocket.SerialPorts:串口通信库,提供了串口通信的封装,使得串口通信更加简单易用。
- log4net:日志库,提供了日志的记录和输出,使得日志记录更加简单易用。