npx skills add https://github.com/dotnet/skills --skill csharp-scripts| 输入 | 必需 | 描述 |
|---|---|---|
| C# 代码或意图 | 是 | 要运行的代码,或对脚本应执行操作的描述 |
运行 dotnet --version 以验证 SDK 是否已安装,并记下主版本号。基于文件的应用程序需要 .NET 10 或更高版本。如果版本低于 10,请改用旧版 SDK 的备用方案。
使用顶级语句创建单个 .cs 文件。将其放在任何现有项目目录之外,以避免与 .csproj 文件冲突。
// hello.cs
Console.WriteLine("Hello from a C# script!");
var numbers = new[] { 1, 2, 3, 4, 5 };
Console.WriteLine($"Sum: {numbers.Sum()}");
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
编写指南:
Main 方法、类或命名空间的样板代码)using 指令放在文件顶部dotnet hello.cs
自动构建并运行文件。已缓存,因此后续运行速度很快。在 -- 之后传递参数:
dotnet hello.cs -- arg1 arg2 "multi word arg"
在文件顶部使用 #:package 指令来引用 NuGet 包。务必指定版本:
#:package Humanizer@2.14.1
using Humanizer;
Console.WriteLine("hello world".Titleize());
用户完成后删除脚本文件。要清除缓存的构建工件:
dotnet clean hello.cs
在 Unix 平台上,使 .cs 文件可直接执行:
在文件的第一行添加 shebang:
#!/usr/bin/env dotnet
Console.WriteLine("I'm executable!");
设置执行权限:
chmod +x hello.cs
直接运行:
./hello.cs
添加 shebang 时,请使用 LF 行尾符(而非 CRLF)。此指令在 Windows 上会被忽略。
基于文件的应用程序默认启用原生 AOT。在 AOT 下,像 JsonSerializer.Serialize<T>(value) 这样基于反射的 API 在运行时会发生故障。请改用源代码生成的序列化:
using System.Text.Json;
using System.Text.Json.Serialization;
var person = new Person("Alice", 30);
var json = JsonSerializer.Serialize(person, AppJsonContext.Default.Person);
Console.WriteLine(json);
var deserialized = JsonSerializer.Deserialize(json, AppJsonContext.Default.Person);
Console.WriteLine($"Name: {deserialized!.Name}, Age: {deserialized.Age}");
record Person(string Name, int Age);
[JsonSerializable(typeof(Person))]
partial class AppJsonContext : JsonSerializerContext;
当脚本规模超出单个文件时,将其转换为完整项目:
dotnet project convert hello.cs
如果 .NET SDK 版本低于 10,则无法使用基于文件的应用程序。请改用临时控制台项目:
mkdir -p /tmp/csharp-script && cd /tmp/csharp-script
dotnet new console -o . --force
用脚本内容替换生成的 Program.cs,并使用 dotnet run 运行。使用 dotnet add package <name> 添加 NuGet 包。完成后删除目录。
dotnet --version 报告 10.0 或更高版本(或使用了备用方案)dotnet build <file>.cs 显式检查)dotnet <file>.cs 产生预期输出| 陷阱 | 解决方案 |
|---|---|
.cs 文件位于包含 .csproj 的目录内 | 将脚本移到项目目录外,或使用 dotnet run --file file.cs |
#:package 未指定版本 | 指定版本:#:package PackageName@1.2.3 或使用 @* 表示最新版 |
| 基于反射的 JSON 序列化失败 | 使用带有 JsonSerializerContext 的源代码生成 JSON(参见源代码生成的 JSON) |
| 意外的构建行为或版本错误 | 基于文件的应用程序会继承父目录中的 global.json、Directory.Build.props、Directory.Build.targets 和 nuget.config。如果继承的设置发生冲突,请将脚本移到隔离的目录 |
有关基于文件的应用程序的完整参考,请参阅 https://learn.microsoft.com/en-us/dotnet/core/sdk/file-based-apps。
每周安装次数
78
代码仓库
GitHub 星标数
703
首次出现
2026年3月10日
安全审计
安装于
opencode75
gemini-cli74
amp74
github-copilot74
kimi-cli74
codex74
| Input | Required | Description |
|---|---|---|
| C# code or intent | Yes | The code to run, or a description of what the script should do |
Run dotnet --version to verify the SDK is installed and note the major version number. File-based apps require .NET 10 or later. If the version is below 10, follow the fallback for older SDKs instead.
Create a single .cs file using top-level statements. Place it outside any existing project directory to avoid conflicts with .csproj files.
// hello.cs
Console.WriteLine("Hello from a C# script!");
var numbers = new[] { 1, 2, 3, 4, 5 };
Console.WriteLine($"Sum: {numbers.Sum()}");
Guidelines:
Main method, class, or namespace boilerplate)using directives at the top of the filedotnet hello.cs
Builds and runs the file automatically. Cached so subsequent runs are fast. Pass arguments after --:
dotnet hello.cs -- arg1 arg2 "multi word arg"
Use the #:package directive at the top of the file to reference NuGet packages. Always specify a version:
#:package Humanizer@2.14.1
using Humanizer;
Console.WriteLine("hello world".Titleize());
Remove the script file when the user is done. To clear cached build artifacts:
dotnet clean hello.cs
On Unix platforms, make a .cs file directly executable:
Add a shebang as the first line of the file:
#!/usr/bin/env dotnet
Console.WriteLine("I'm executable!");
Set execute permissions:
chmod +x hello.cs
Run directly:
./hello.cs
Use LF line endings (not CRLF) when adding a shebang. This directive is ignored on Windows.
File-based apps enable native AOT by default. Reflection-based APIs like JsonSerializer.Serialize<T>(value) fail at runtime under AOT. Use source-generated serialization instead:
using System.Text.Json;
using System.Text.Json.Serialization;
var person = new Person("Alice", 30);
var json = JsonSerializer.Serialize(person, AppJsonContext.Default.Person);
Console.WriteLine(json);
var deserialized = JsonSerializer.Deserialize(json, AppJsonContext.Default.Person);
Console.WriteLine($"Name: {deserialized!.Name}, Age: {deserialized.Age}");
record Person(string Name, int Age);
[JsonSerializable(typeof(Person))]
partial class AppJsonContext : JsonSerializerContext;
When a script outgrows a single file, convert it to a full project:
dotnet project convert hello.cs
If the .NET SDK version is below 10, file-based apps are not available. Use a temporary console project instead:
mkdir -p /tmp/csharp-script && cd /tmp/csharp-script
dotnet new console -o . --force
Replace the generated Program.cs with the script content and run with dotnet run. Add NuGet packages with dotnet add package <name>. Remove the directory when done.
dotnet --version reports 10.0 or later (or fallback path is used)dotnet build <file>.cs)dotnet <file>.cs produces the expected output| Pitfall | Solution |
|---|---|
.cs file is inside a directory with a .csproj | Move the script outside the project directory, or use dotnet run --file file.cs |
#:package without a version | Specify a version: #:package PackageName@1.2.3 or @* for latest |
| Reflection-based JSON serialization fails | Use source-generated JSON with JsonSerializerContext (see Source-generated JSON) |
| Unexpected build behavior or version errors |
See https://learn.microsoft.com/en-us/dotnet/core/sdk/file-based-apps for a full reference on file-based apps.
Weekly Installs
78
Repository
GitHub Stars
703
First Seen
Mar 10, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode75
gemini-cli74
amp74
github-copilot74
kimi-cli74
codex74
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
122,000 周安装
LobeHub Modal命令式API指南:React模态框实现与最佳实践
544 周安装
Accord Project 智能合同模板生成器 - 自动化创建法律合同与协议模板
558 周安装
GitHub Release自动化发布工具 - 安全检查与版本发布工作流
546 周安装
ess-dev技能:MCP Hub代理技能目录,每周安装量69+,支持多平台开发工具
69 周安装
remind-me 自然语言提醒工具 - 使用 cron 和 Markdown 设置自动化提醒
542 周安装
GitHub PR讲解视频生成工具 - 为拉取请求自动创建代码变更讲解视频
69 周安装
File-based apps inherit global.json, Directory.Build.props, Directory.Build.targets, and nuget.config from parent directories. Move the script to an isolated directory if the inherited settings conflict |