我有一个Project
参考,并且正在通过Document
向该项目添加新的AddDocument()
。问题是,无论我向呼叫发送什么内容,新文件总是以项目的根目录结尾。我试过在文件的name
前面加上一个子目录:
project.AddDocument(@"\GoHere\MyNewFile.cs", ...)
但这不起作用。这也没有:
project.AddDocument(@"\GoHere\MyNewFile.cs", ..., filePath: @"C:\ProjectPath\GoHere");
我也尝试了其他选择,但无济于事。编译器API中是否可以通过某种方式将文件添加到不存在的新子目录中的项目中?如果是这样,怎么办? TIA。
最佳答案
我认为您只需要在folders
上使用可选参数AddDocument
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);
似乎正确添加了文档:
关于c# - 在Roslyn项目中创建一个新文件夹,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34955015/