Roslyn代码生成的文件需要保存自定义位置

时间:2019-04-10 11:52:49

标签: code-generation filepath roslyn

我需要将保存文件位置更改为另一个目录文件夹。 现在,它仅保存当前的根文件夹。  你能帮我吗?。

var tree = CSharpSyntaxTree.ParseText(code);             
            var root = await tree.GetRootAsync().ConfigureAwait(false) as CompilationUnitSyntax;
            var oldNamespace = root.Members.Single(m => m is NamespaceDeclarationSyntax) as NamespaceDeclarationSyntax;
            var classDeclarations = oldNamespace.Members.Where(m => m is ClassDeclarationSyntax);
            var newNamespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(@namespace)).NormalizeWhitespace();
            newNamespace = newNamespace.AddMembers(classDeclarations.Cast<MemberDeclarationSyntax>().ToArray());
            root = root.ReplaceNode(oldNamespace, newNamespace).NormalizeWhitespace();
            string newCode = root.ToFullString();                    
            File.WriteAllText(Test.cs, root.ToFullString());

1 个答案:

答案 0 :(得分:0)

这行不通吗

File.WriteAllText("C:\path\to\thing\Test.cs", root.ToFullString());