-1

In this current Solution I will have ~10 projects (Console Applications) which will use the same functions from Helper classes. In the past I would just copy paste those folders in each project and add the references to the main class. Is there a way to create the Helper classes only once and be able to use them in any project in that solution, without duplicating them or having to reference another project?

Also I have to import the same 3 NuGet packages in each project, is there a way to import them at solution level and so they will be automatically added in each new project? I know I can right-click the Solution and just select each project without doing it manually for each project, but can they be added automatically for each new project created?

Victor Valeanu
  • 398
  • 6
  • 20
  • You have asked two unrelated questions here, which is stratifying against site rules. If you have two questions to ask, post two separate questions. – jmcilhinney Oct 08 '22 at 14:29

2 Answers2

1

You need edit csproj, for example you have following directory structure:

SolutionRoot
  MyProject
    MyProject.csproj
  SharedFiles

To add files in SharedFiles directory into MyProject project as a virtual folder named Shared, add following lines into MyProject.csproj

<ItemGroup>
   <Compile Include="..\SharedFiles\**\*.cs">
      <Link>Shared\%(RecursiveDir)\%(FileName)%(Extension)</Link>
   </Compile>
</ItemGroup>
shingo
  • 18,436
  • 5
  • 23
  • 42
  • I ended up creating a separate Project named Helpers and used this line in every other project "using Helpers.Helpers". I think it does the same thing as your configuration, but it is less of a headache, or am I missing some other benefits from your approach ? (In my case I only have one class in each project where this needs to be imported, so maybe your approach is more work, but more efficient if I would have used it in more classes from each project) – Victor Valeanu Oct 08 '22 at 16:43
  • Not much benefit but you can use partial or internal class in the project. And this is what you said in the question: _"without duplicating them or having to reference another project"_. – shingo Oct 09 '22 at 03:57
0

Is there a way to create the Helper classes only once and be able to use them in any project in that solution, without duplicating them or having to reference another project?

Simple answer no. You must define your helper classes in a project that can be referenced in your other console applications.

Also I have to import the same 3 NuGet packages in each project, is there a way to import them at solution level and so they will be automatically added in each new project? I know I can right-click the Solution and just select each project without doing it manually for each project, but can they be added automatically for each new project created?

Again no, you can't apply NuGet references to new projects at a solution level. However, you can create your own project template which can be setup to have package/project references configured.

Hayden
  • 2,902
  • 2
  • 15
  • 29
  • You've answered two unrelated questions here. Posting answers to bad questions encourages more bad questions. You should have promoted the OP to separate them and then answered them separately. – jmcilhinney Oct 08 '22 at 14:31
  • I ended up going with the Helpers project and referencing it in all other projects, I will accept your answer as the solution as you also pointed me in the right direction with the project template feature. Thanks! – Victor Valeanu Oct 08 '22 at 16:45