© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
27 replies
Humoroussix2799

✅ Is there a way to programmatically formatt a file

Im writing a transpiler to C# (in C#) and I would like to formatt the output files after I write them to C# does anybody know how to do this? Roslyn does not work:
        private async void FormatCode(string path)
        {
            
            string code = File.ReadAllText(path);
            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
            var root = await syntaxTree.GetRootAsync();
            var formattedRoot = Formatter.Format(root, Formatter.Annotation, new AdhocWorkspace());
            string formattedCode = formattedRoot.ToFullString();
            File.WriteAllText(path, formattedCode);
        }
        private async void FormatCode(string path)
        {
            
            string code = File.ReadAllText(path);
            SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(code);
            var root = await syntaxTree.GetRootAsync();
            var formattedRoot = Formatter.Format(root, Formatter.Annotation, new AdhocWorkspace());
            string formattedCode = formattedRoot.ToFullString();
            File.WriteAllText(path, formattedCode);
        }

SOLUTION FOUND by SleepWellPupper
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,828Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ Is there a way to override the DefaultInterpolatedStringHandler?
C#CC# / help
3y ago
is there is a way to make this faster
C#CC# / help
3y ago
❔ Is there a way to get a Private field?
C#CC# / help
3y ago
Is there a faster way to map endpoints?
C#CC# / help
7mo ago