Search code examples
c#visual-studioresharperautoformatting

autoformat code from command line


Is it possible to run auto-format code for all or for specific file in solution, like (Ctrl+K, Ctrl+D) formatting in Visual Studio but from it`s command line? Or use Resharper's cleanup also from command line for solution files?


Solution

  • To format net core c# source, use https://github.com/dotnet/format

    Install the tool as per the project readme.

    I had a need to format some code files I was generating from Razor templates. I created a shell .CSProj file in the root of my output folder, using dotnet new console which gives you this basic file:

    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp2.2</TargetFramework>
        <RootNamespace>dotnet_format</RootNamespace>
      </PropertyGroup>
    
    </Project>
    

    Then run dotnet format from a VS command prompt in that folder. It will recurse into sub-directories and format everything it finds. To format specific files you can provide a list of filenames with the --files switch.