I know it is not a "solution"-wide, but I tried to find the any way to run script on project build, so may be helpful for people, that would like any C# script to, for example, modify solution/project files before/after build.
So, as @Hans Passant posted above - we can create new Console Application and run this .exe on build event. Please, read comments to his post, as it is only applicable in case you don't need to integrate this logic to CI/CD and etc. So usage some limited. And in general, it is not the most beautiful solution.
So, for example, we have any project (AwsLambdaProject) and you need to modify .env file inside on each build to update internal variables.
- create new C# Application console (in my case it's name :
PreBuildEventApp)
- build it and open properties of your target AwsLambdaProject
- find build->events tab in project properties. And type there "$(SolutionDir)\PreBuild\PreBuildEventApp\bin\Debug\net6.0\PreBuildEventApp.exe -solutionPath=$(SolutionDir)"
So here we are calling our .exe and passing there one parameter (solution folder path).
*Check, that path contains your valid console app .exe file.
So, your .exe will be executed before every "AwsLambdaProject" build and modify required files.
So for example, you try to write something to file to debug it :

And your file will be created in AwsLambdaProject project folder, not in console project/bin folder.
Notes :
To use async inside your console app, be sure, that AwsLambdaProject contains 7.1 or higher (i use 10.0).

Also, you may face with issue, when first build is failed and only second time it will be succeed. It is because you need to build you console app first to have .exe in right place. You can copy it manually to any folder - it will work fine. But if you need to change code - you need to do it again.
The second way is to click to solution and click "Project Build order", there you can find your "AwsLambdaProject" and add your console app as a dependency. Now, your console app will build first and your project where you need to modify files - second.
Also, you may faced with another one issue here - build event will work only on first build or on Solution Rebuild. To call your console app on every solution build - you need to modify two projects and add there :
<PropertyGroup>
<DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
</PropertyGroup>
It means - your two projects will build every time, without caching. Be careful, it may increase your usual build time on solution build, depending on project size.
Hope, will help someone. Have a good day!