A .NET tool to organize C# code files using Roslyn
$ dotnet add package CSharplyAn opinionated tool that organizes C# files according to best practices. Available as a CLI tool, web API, and VS Code extension.
dotnet tool install --global CSharply
Install from the Visual Studio Marketplace or search for "CSharply" in VS Code extensions.
csharply organize MyClass.cs
csharply organize ./src
csharply --help
csharply organize --help
Start a web server to organize code via HTTP API:
csharply serve
csharply serve --port 8149
GET /health - Health checkPOST /organize - Organize C# code (plain text body)# Organize code via plain text
curl -X POST http://localhost:8149/organize \
-H "Content-Type: text/plain" \
-d "using System.Linq; using System; class Test { }"
Ctrl+Shift+P, then CSharply: Organize C# file or CSharply: Organize all C# files in workspace folders
CSharply organizes your C# code according to Microsoft's coding conventions:
Member Order:
Access Modifier Order:
Note: If the file contains pre-processor directives such as #if or #region, the file will not be organized.
Before:
using System.Collections.Generic;
using System.Linq;
using System;
namespace MyProject
{
public class Example
{
public void DoSomething1() { }
private string _field1;
public void DoSomething2() { }
public Example() { }
public string Property { get; set; }
private string _field2;
}
}
After:
using System;
using System.Collections.Generic;
using System.Linq;
namespace MyProject
{
public class Example
{
private string _field1;
private string _field2;
public Example() { }
public string Property { get; set; }
public void DoSomething1() { }
public void DoSomething2() { }
}
}
CSharply supports a .csharplyignore file to exclude specific files and directories from organization. This is useful for:
Create a .csharplyignore file in your project root or any directory you want to organize. The file uses gitignore-style patterns:
# Ignore all generated files
**/Generated/
**/*.Designer.cs
**/*.g.cs
# Ignore specific files
Models/LegacyModel.cs
Controllers/ThirdPartyController.cs
# Ignore by pattern
**/*Template*.cs
**/Migrations/**/*.cs
# Ignore entire directories
bin/
obj/
packages/
The .csharplyignore file supports the same globbing patterns as .gitignore:
| Pattern | Description | Example |
|---|---|---|
*.cs | Match any .cs file | Generated.cs, Model.cs |
**/Generated/ | Match Generated directory anywhere | src/Generated/, test/Generated/ |
Models/*.cs | Match .cs files in Models directory | Models/User.cs |
**/*.Designer.cs | Match Designer.cs files anywhere | Form1.Designer.cs |
!Important.cs | Negation - don't ignore this file | Override previous ignore rules |
.csharplyignore files starting from the target directoryMyProject/
├── .csharplyignore # Root ignore file
├── src/
│ ├── .csharplyignore # Source-specific ignores
│ ├── Controllers/
│ ├── Models/
│ └── Generated/ # Ignored directory
├── tests/
└── bin/ # Ignored directory
# Build outputs
bin/
obj/
publish/
# Generated files
**/*.Designer.cs
**/*.g.cs
**/Migrations/*.cs
# Third-party code
**/ThirdParty/
**/External/
# Designer files
**/*.Designer.cs
**/*.g.cs
# Build outputs
bin/
obj/
# Legacy code
**/Legacy/
**/Old/
Use the --verbose flag to see which files are being ignored:
csharply organize ./src --verbose
# Output will show:
# skipped : src/Generated/Model.cs
# organized : src/Controllers/UserController.cs
csharply organize [options] <path>
Options:
--simulate, -s Simulate changes without writing files
--verbose, -v Enable verbose output
--help, -h Show help information
csharply server [options]
Options:
--port <port> Port to listen on (default: 8149)
--help, -h Show help information