在 VisualStudio 中将所有 *.cs 文件转换为 unicode [英] Convert all *.cs files to unicode in VisualStudio

查看:14
本文介绍了在 VisualStudio 中将所有 *.cs 文件转换为 unicode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队不关注文件编码(这是正确的,因为人类不应该被文件编码所困扰).但是有些文件以utf8格式保存,有些文件以区域编码(cp1250)保存.

My team does not pay attention to file encoding (and that is correct, because humans should not be bothered by file encodings). However some files are saved in utf8, and some in regional encoding (cp1250).

我需要一个可以做两件事的工具:

I need a tool that can do two things:

如何使用 Visual-Studio 或 Resharper 插件和/或外部工具实现这些目标?

How can I achieve these goals using Visual-Studio or Resharper plugins, and or external tools?

我尝试使用 PowerShell 执行 #2,但是它很混乱(有时它会删除/添加最后一行).可能有一些免费软件可以用来做这件事,第 1 点对我来说更重要.

I tried to do #2 with PowerShell, however it is mess (sometimes it removes/adds last line). Probably there is some free software that I can use to do it, point #1 is more important for me.

推荐答案

是的,这是可能的.

使用 .editorconfig 作为 @Richard 前面提到的.从 Visual Studio v15.3 开始,.editorconfig 支持得到修复和改进.这个解决方案级别的简单 .editorconfig 足以确保每个 *.cs 都保存在 UTF-8 中,没有 BOM:

Use .editorconfig as @Richard previously mentioned. Starting from Visual Studio v15.3, .editorconfig support was fixed and improved. This simple .editorconfig at the solution level would be enough to ensure each *.cs is saved in UTF-8 without BOM:

root = true

[*.cs]
charset = utf-8

此外,它还可以转换由 Visual Studio 手动打开和保存的任何现有文件.

Moreover, it converts any existing file manually opened and saved by Visual Studio.

我从线程中测试了一些答案保存所有Visual Studio 项目中的文件为 UTF-8 并且它们工作得很糟糕:非拉丁字符(在我的情况下是西里尔文)已转换为不可读的字形.相反,Visual Studio 本身可以完美地进行打开-保存"转换.

I tested some answers from the thread Save all files in Visual Studio project as UTF-8 and they worked badly: non-Latin characters (Cyrillic in my case) had been converted into unreadable glyphs. On the contrary, Visual Studio itself does the "open-save" conversion flawlessly.

要自动打开并重新保存解决方案中的所有代码文件,请使用一个简单的 R# 技巧:

To automatically open and re-save all code files in a solution, use a simple R# trick:

  1. 将适用于所有文件的任何 R# 代码样式规则设置为严格拒绝贵公司代码约定的值.例如,大括号布局是一个显而易见的选择.
  2. 使用代码清理将其应用于整个解决方案功能(默认为Ctrl+E,C).选择一个最简单的内置重新格式化代码"模板以最大限度地减少更改.
  3. 格式化并保存所有文件后,将 R# 规则恢复为原始规则并再次运行 代码清理.
  1. Set any R# code style rule appllicable to all your files to the value which strictly denies your company's code conventions. For example, braces layout is an obvious choice.
  2. Apply it to the whole solution using a Code Cleanup feature (Ctrl+E,C by default). Choose a simplest built-in "Reformat Code" template to minimize changes.
  3. After all files have been formatted and saved, revert the R# rules back to their originals and run Code Cleanup once again.

之后,您所有的 *.cs 文件都应该以 UTF-8 格式保存(对于 R# 支持的其他文件类型也是如此).漂亮的格式作为奖励.

All your *.cs files should be saved in UTF-8 after that (the same idea for another file types supported by R#). Pretty formatting as a bonus.

这篇关于在 VisualStudio 中将所有 *.cs 文件转换为 unicode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆