gpt4 book ai didi

c# - 在 Roslyn 项目中创建新文件夹

转载 作者:行者123 更新时间:2023-11-30 16:04:29 26 4
gpt4 key购买 nike

我有一个 Project 引用,我正在通过 AddDocument() 向该项目添加一个新的 Document。问题是,无论我在调用中发送什么,新文件总是在项目的根目录中结束。我试过在文件的 name 前加上一个子目录:

project.AddDocument(@"\GoHere\MyNewFile.cs", ...)

但这行不通。这也不是:

project.AddDocument(@"\GoHere\MyNewFile.cs", ..., filePath: @"C:\ProjectPath\GoHere");

我也尝试过其他选择,但都无济于事。编译器 API 中是否有某种方法可以将文件添加到不存在的新子目录中的项目?如果是这样,如何? TIA。

最佳答案

我认为您只需要在 AddDocument 上使用可选参数 folders

SourceText text; 
List<string> folders = new List<string>() { "GoHere" };
project.AddDocument(@"MyNewFile.cs", text, folders: folders);

因为你提到了 VS,所以我只是从 VSPackage 的 Initialize() 仔细检查了这个。

var componentModel = (IComponentModel)Microsoft.VisualStudio.Shell.Package.GetGlobalService(typeof(SComponentModel));
var workspace = componentModel.GetService<VisualStudioWorkspace>();

var sourceText = SourceText.From("Test");
var folders = new List<string>() { "GoHere" };
var proj = workspace.CurrentSolution.Projects.Single();
var document = proj.AddDocument("Test.cs", sourceText, folders);

var result = workspace.TryApplyChanges(document.Project.Solution);

似乎正确添加了文档:

enter image description here

关于c# - 在 Roslyn 项目中创建新文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34955015/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com