是否可以在解决方案中运行自动格式化代码,例如在Visual Studio中使用(Ctrl+K,Ctrl+D)格式化,但是通过命令行操作? 或者从命令行使用Resharper的cleanup来清理解决方案文件?
是否可以在解决方案中运行自动格式化代码,例如在Visual Studio中使用(Ctrl+K,Ctrl+D)格式化,但是通过命令行操作? 或者从命令行使用Resharper的cleanup来清理解决方案文件?
EnvDTE
,EnvDTE80
来创建Visual Studio项目,并即时加载要格式化的文件。完成后删除Visual Studio项目。您可以指定在格式化时不显示Visual Studio窗口。如果您感兴趣,请告诉我,我可以给您一些代码来使其工作。 //You need to make a reference to two dlls:
envdte
envdte80
void FormatFiles(List<FileInfo> files)
{
//If it throws exeption you may want to retry couple more times
EnvDTE.Solution soln = System.Activator.CreateInstance(Type.GetTypeFromProgID("VisualStudio.Solution.11.0")) as EnvDTE.Solution;
//try this if you have Visual Studio 2010
//EnvDTE.Solution soln = System.Activator.CreateInstance(Type.GetTypeFromProgID("VisualStudio.Solution.10.0")) as EnvDTE.Solution;
soln.DTE.MainWindow.Visible = false;
EnvDTE80.Solution2 soln2 = soln as EnvDTE80.Solution2;
//Creating Visual Studio project
string csTemplatePath = soln2.GetProjectTemplate("ConsoleApplication.zip", "CSharp");
soln.AddFromTemplate(csTemplatePath, tempPath, "FormattingFiles", false);
//If it throws exeption you may want to retry couple more times
Project project = soln.Projects.Item(1);
foreach (FileInfo file in files)
{
ProjectItem addedItem;
bool existingFile = false;
int _try = 0;
while (true)
{
try
{
string fileName = file.Name;
_try++;
if (existingFile)
{
fileName = file.Name.Substring(0, (file.Name.Length - file.Extension.Length) - 1);
fileName = fileName + "_" + _try + file.Extension;
}
addedItem = project.ProjectItems.AddFromTemplate(file.FullName, fileName);
existingFile = false;
break;
}
catch(Exception ex)
{
if (ex.Message.Contains(file.Name) && ex.Message.Contains("already a linked file"))
{
existingFile = true;
}
}
}
while (true)
{
//sometimes formatting file might throw an exception. Thats why I am using loop.
//usually first time will work
try
{
addedItem.Open(Constants.vsViewKindCode);
addedItem.Document.Activate();
addedItem.Document.DTE.ExecuteCommand("Edit.FormatDocument");
addedItem.SaveAs(file.FullName);
break;
}
catch
{
//repeat
}
}
}
try
{
soln.Close();
soln2.Close();
soln = null;
soln2 = null;
}
catch
{
//for some reason throws exception. Not all the times.
//if this doesn't closes the solution CleanUp() will take care of this thing
}
finally
{
CleanUp();
}
}
void CleanUp()
{
List<System.Diagnostics.Process> visualStudioProcesses = System.Diagnostics.Process.GetProcesses().Where(p => p.ProcessName.Contains("devenv")).ToList();
foreach (System.Diagnostics.Process process in visualStudioProcesses)
{
if (process.MainWindowTitle == "")
{
process.Kill();
break;
}
}
tempPath = System.IO.Path.GetTempPath();
tempPath = tempPath + "\\FormattingFiles";
new DirectoryInfo(tempPath).Delete(true);
}
static void FormatFile(string file)
{
EnvDTE.Solution soln = System.Activator.CreateInstance(
Type.GetTypeFromProgID("VisualStudio.Solution.10.0")) as EnvDTE.Solution;
soln.DTE.ItemOperations.OpenFile(file);
TextSelection selection = soln.DTE.ActiveDocument.Selection as TextSelection;
selection.SelectAll();
selection.SmartFormat();
soln.DTE.ActiveDocument.Save();
}
SmartFormat
时会抛出错误。我还实现了MessageFilter
。 - Patrick MageeTools.CommandWindow
并使用了document.Activate();
接着是CommandWindow.SendInput("Edit.FormatDocument")
。 - Patrick Magee若要格式化.NET Core C#源代码,请使用 https://github.com/dotnet/format
按照项目的自述文件安装工具。
我需要对从Razor模板生成的一些代码文件进行格式化。我在输出文件夹的根目录下创建了一个外壳.CSProj文件,使用dotnet new console
命令可以得到以下基本文件:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
<RootNamespace>dotnet_format</RootNamespace>
</PropertyGroup>
</Project>
然后在该文件夹中的 VS 命令提示符中运行 dotnet format
命令。它会递归地进入子目录并格式化找到的所有内容。要格式化特定的文件,可以使用 --files
开关并提供文件名列表。
CodeFormatter.csproj
添加到您的项目的根目录中:CodeFormatter.csproj
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="**\*.cs" />
</ItemGroup>
<Target Name="Compile">
<Csc Sources="@(Compile)"/>
</Target>
</Project>
> codeformatter.exe CodeFormatter.csproj /nocopyright
CodeFormatter.csproj
添加到根目录会递归包含所有 C# 文件,这意味着上述内容适用于基于 project.json 和 *.xproj 的设置。