c# - Visual Studio Code Task 能否生成文件并在同一窗口中自动打开
问题描述
我有一个 C# 任务,它在我的 VSCode 工作区文件夹中生成一个文件。
我希望文件在生成后自动打开。目前我能做的最好的事情是打印文件的全名,以便用户可以Ctrl+单击链接。
环境设置:
- 在空文件夹中打开 VSCode。
- 创建一个新的 C# 控制台应用程序(
dotnet new console
在终端中键入)。 - 更新
Program.cs
:
namespace VSCodeOpenGeneratedFile
{
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
File.WriteAllText("test.txt", "Hello world!");
Console.WriteLine(Path.Combine(Directory.GetCurrentDirectory(), "test.txt"));
}
}
}
- 添加
.vscode/tasks.json
:
{
"version": "2.0.0",
"tasks": [
{
"label": "Generate File",
"command": "dotnet run",
"type": "shell",
"problemMatcher": []
}
]
}
- 运行任务:
- Ctrl+ Shift+P
- 选择任务:运行任务
- 选择生成文件
- 在终端中,您应该会看到文件名,您可以Ctrl+单击打开该文件名。
如何让任务自动打开此文件?
解决方案
我发现您可以使用VS code 的命令行参数来完成此操作。使用-r
or--reuse-window
参数,您可以在当前的 VS 代码窗口中打开一个文件。
在使用此任务创建过时的降价文件后,我能够做到这一点:
{
"label": "new note",
"type": "shell",
"command": "set -lx t (date +%Y-%m-%d-%H-%M-%S);echo \"# $t\" > ${workspaceFolder}/notes/$t.md;code -r ${workspaceFolder}/notes/$t.md"
}
这是在带有 fish shell 的 Linux 上,但您应该能够对 Windows 或其他 shell 使用类似的命令。
推荐阅读
- python - 使用 Python 配置解析器包的问题,我们无法在另一个类文件中读取 init 文件的内容
- css - 样式 v-html Vue 3
- nats.io - NATS 最大集群大小
- python - Matplotlib.collections.PathCollection
- javascript - 控制台错误:index.js:1 警告:列表中的每个孩子都应该有一个唯一的“key”道具
- html - 在 FullCalendar v5 中包装标题
- authentication - 在 localStorage 的标头中设置 JWT 令牌以进行重定向
- javascript - 我需要使用 localStorage 的帮助
- python - PyCharm - 来自外部范围的阴影名称
- prometheus - [普罗米修斯]普罗米修斯中的目标宕机后再也回不来会发生什么