Home > Mobile >  Insert multiple text lines in multiple code files at once in VS
Insert multiple text lines in multiple code files at once in VS

Time:08-17

I need to know if there is a faster way than manually copy-pasting 4 new libraries in every code file that I have. After a major refactoring and addition of new libraries , I need to include these in every code file :

using GameServerCore.Enums;
using GameServerCore.Scripting.CSharp;
using System.Numerics;
using GameServerLib.GameObjects.AttackableUnits;

I cant use Replace All because I cant replace anything to fit those, and manually copy-pasting them will take me ages because there are hundreds of .cs files.

Using Visual Studio 2022, all script files are written in C#

CodePudding user response:

Starting with C# 10 you can use global usings:

global using GameServerCore.Enums;
global using GameServerCore.Scripting.CSharp;
global using System.Numerics;
global using GameServerLib.GameObjects.AttackableUnits;

Add these global usings in one file in your project, e.g. "GlobalUsings.cs" and they will be active in all the *.cs files.

The docs for using directive say:

The global modifier has the same effect as adding the same using directive to every source file in your project. This modifier was introduced in C# 10.

According to Visual Studio 2022 version 17.3 Release Notes:

We now surface an icon at the top of your file to let you know if Global Usings are active in your file and if you click on the icon, it will show you what those Global Usings are.

The article C# language versioning explains how you can activate newer C# versions in older framework versions.

  • Related