1. 程式人生 > >使用 Visual Studio 2019 批量新增程式碼檔案頭

使用 Visual Studio 2019 批量新增程式碼檔案頭

## 應用場景介紹 在我們使用一些開源專案時,基本上都會在每個原始碼檔案的頭部看到一段版權宣告。一個專案或解決方案中原始碼檔案的個數少則幾十,多則幾千甚至更多,那麼怎麼才能給這麼多檔案方便地批量新增或者修改一致的檔案頭呢?在 2020 年 8 月 11 日 更新的 *Visual Studio 2019* **v16.7.1** 版中,就加入了*使用 EditorConfig 向現有檔案、專案和解決方案新增檔案頭* 這一功能。提到 *EditorConfig*, 那麼: ## 什麼是 EditorConfig? `EditorConfig` 是幫助跨多個編輯器和 IDE 的從事同一專案的多個開發人員保持一致性編碼風格的一個文字檔案。EditorConfig 檔案可以設定諸如縮排樣式、選項卡寬度、行尾字元以及編碼等,而無需考慮使用的編輯器或 IDE。向專案新增 EditorConfig 檔案,可以強制對使用該專案的所有人員實施一致的編碼風格。 EditorConfig 設定優先於全域性 Visual Studio 文字編輯器設定。 由於這些設定包含在基本程式碼的檔案中,因此能與基本程式碼一起移動。 只要在 EditorConfig 相容的編輯器中開啟程式碼檔案,就能實現文字編輯器設定。 有關 EditorConfig 檔案的詳細資訊,請參閱 [EditorConfig.org](https://editorconfig.org/) 網站。 ## 使用 Visual Studio 2019 新增檔案頭 首先,在 Visual Studio 中開啟專案或解決方案,點選“幫助” > “關於”,檢視您的 Visual Studio 2019 版本是否是 `16.7.1` 或更高版本,如果不是請先升級。 1. 向專案或解決方案新增 EditorConfig。 根據要應用 `.editorconfig` 設定的物件(是解決方案中的所有專案還是其中一個專案),選擇專案或解決方案節點。 還可在專案或解決方案中選擇一個資料夾,向其新增 `.editorconfig` 檔案。 從選單欄中,選擇“專案” > “新增新項”,或按 Ctrl+Shift+A : ![vs_file_header_template_0](https://img2020.cnblogs.com/blog/2074831/202009/2074831-20200902021512432-457849230.png#center) 或者在“解決方案資源管理器”中右鍵單擊專案、解決方案或資料夾,選擇“新增” > “新建 EditorConfig”: ![vs_file_header_template_1](https://img2020.cnblogs.com/blog/2074831/202009/2074831-20200902021550203-1510489980.png#center) 新增完成後可以看到 `.editorconfig` 檔案的內容大致如下: ![vs_file_header_template_2](https://img2020.cnblogs.com/blog/2074831/202009/2074831-20200902021734515-1575337819.png#center) 2. 在 `.editorconfig` 檔案中新增 `file_header_template` 項 ``` file_header_template = 新增檔案頭(add file header)示例程式\n Copyright (c) https://ittranslator.cn/ ``` > `.editorconfig` 中換行需要使用 Unix 換行符(`\n`)來插入新行。 如圖: ![vs_file_header_template_3](https://img2020.cnblogs.com/blog/2074831/202009/2074831-20200902021814523-761936056.png#center) 3. 將游標置於任意 C# 或 Visual Basic 檔案的第一行,按 `Ctrl+.` 觸發“快速操作和重構”選單,選擇“新增檔案頭”,如圖: ![vs_file_header_template_4](https://img2020.cnblogs.com/blog/2074831/202009/2074831-20200902021855034-1899654219.png#center) 在“修復以下物件中的所有例項:”處可以選擇 “文件”、“專案”或“解決方案”,下圖是選擇了“專案”後的預覽效果: ![vs_file_header_template_5](https://img2020.cnblogs.com/blog/2074831/202009/2074831-20200902021931951-728791126.png#center) 4. 點選“應用”按鈕,以應用更改。 此時可以看到專案中的所有程式碼檔案都添加了 `.editorconfig` 中設定的 `file_header_template` 項的字串作為檔案頭: ![vs_file_header_template_6](https://img2020.cnblogs.com/blog/2074831/202009/2074831-20200902022008303-621461223.png#center) ## 修改檔案頭 修改檔案頭和新增檔案頭的步驟是一樣的。 1. 在 `.editorconfig` 檔案中修改 `file_header_template` 項的值 新增“技術譯站”幾個文字試試: ``` file_header_template = 新增檔案頭(add file header)示例程式\n Copyright (c) 技術譯站 https://ittranslator.cn/ ``` 2. 重複上面“新增檔案頭”的第 3 步,預覽如下圖: ![vs_file_header_template_7](https://img2020.cnblogs.com/blog/2074831/202009/2074831-20200902022043319-1498876344.png#center) 3. 重複上面“新增檔案頭”的第 4 步,點選“應用”按鈕,結果如下圖: ![vs_file_header_template_8](https://img2020.cnblogs.com/blog/2074831/202009/2074831-20200902022114959-89605525.png#center) ## 參考文獻 - [https://docs.microsoft.com/zh-cn/visualstudio/ide/create-portable-custom-editor-options?view=vs-2019](https://docs.microsoft.com/zh-cn/visualstudio/ide/create-portable-custom-editor-options?view=vs-2019) - [https://docs.microsoft.com/zh-cn/visualstudio/ide/reference/add-file-header?view=vs-2019](https://docs.microsoft.com/zh-cn/visualstudio/ide/reference/add-file-header?view=vs-2019)
> 作者 : 技術譯民 > 出品 : [技術譯站](https://ittranslator.cn/)