How can I create a clean project folder structure?

fatih uyanık 80 Reputation points
2024-09-11T07:10:33.9366667+00:00

Hello

I use many libraries that I downloaded from Nugget in a wpf c# project. These libraries are normally located under the "Bin" folder.

I want to create a cleaner folder structure. For this, I want to collect these libraries under a folder like "Lib" and have a cleaner structure in the project folder.

How can I create this clean structure so that the project can work without problems on other computers when it uses the libraries it references? I have seen many methods on the internet, but I could not understand which one would work better.

I ask for your help on this issue.

Thank you very much in advance.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,762 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,858 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Hongrui Yu-MSFT 1,605 Reputation points Microsoft Vendor
    2024-09-11T10:10:48.6266667+00:00

    Hi,@fatih uyanık. Welcome to Microsoft Q&A. 

    I take CommunityToolkit.Mvvm as an example. I downloaded CommunityToolkit.Mvvm through NuGet and set its output location to the Lib folder when the project is generated.

     

    First, move the CommunityToolkit.Mvvm.dll location to the Lib file through the Post-build event:

    WPF Application

    Right click on the project-> Properties ->Build->Events->Post-build event->Enter the following code

    
    mkdir  $(OutputPath)\Lib
    
    move $(OutputPath)\CommunityToolkit.Mvvm.dll  $(OutputPath)\Lib\CommunityToolkit.Mvvm.dll
    

          

    WPF App(.NET Framework)

    Right click on the project->Properties->Build-Events->Post-build event command line->Enter the following code

    
    mkdir Lib
    
    move CommunityToolkit.Mvvm.dll  Lib\CommunityToolkit.Mvvm.dll
    
    

     

    Then reload the assembly:

    WPF Application

    Double-click the project and add <RuntimeHostConfigurationOption Include="SubdirectoriesToProbe" Value="Lib" /> in ItemGroup

    
    <ItemGroup>
    
    <RuntimeHostConfigurationOption Include="SubdirectoriesToProbe" Value="Lib" />
    
      <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />
    
    </ItemGroup>
    
    

     

    Add the following code in App.xaml.cs

    
    public partial class App : Application
    
    {
    
        protected override void OnStartup(StartupEventArgs e)
    
        {
    
            //Load Assembly Event
    
            AssemblyLoadContext.Default.Resolving += ResolveAssembly;
    
     
    
            base.OnStartup(e);
    
        }
    
     
    
        // Load the specified location assembly
    
        private static Assembly ResolveAssembly(AssemblyLoadContext assemblyLoadContext, AssemblyName assemblyName)
    
        {
    
            var probeSetting = AppContext.GetData("SubdirectoriesToProbe") as string;
    
            if (string.IsNullOrEmpty(probeSetting))
    
            {
    
                return null;
    
            }
    
     
    
            foreach (var subDirectory in probeSetting.Split(';'))
    
            {
    
                var pathMaybe = Path.Combine(AppContext.BaseDirectory, subDirectory, $"{assemblyName.Name}.dll");
    
                if (File.Exists(pathMaybe))
    
                {
    
                    return assemblyLoadContext.LoadFromAssemblyPath(pathMaybe);
    
                }
    
            }
    
     
    
            return null;
    
        }
    
    }
    
    

     

    WPF App(.NET Framework)

    Add the following code to <configuration> in App.config.

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
           …
           <runtime>
              <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
                     <probing privatePath="Lib;"/>
              </assemblyBinding>
           </runtime>
    </configuration>
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. altaan 0 Reputation points
    2024-09-11T13:41:56.46+00:00

    To organize your libraries in a custom "Lib" folder in a WPF C# project, follow these steps:

    1. Create a "Lib" Folder: Manually create a "Lib" folder in your project directory where you want to store the libraries.
    2. Move the DLLs: Move all the necessary NuGet package DLLs from the "bin" folder to your new "Lib" folder.
    3. Update References: In Visual Studio, right-click on your project, go to "References," and remove the old references. Then, add new references pointing to the DLLs in your "Lib" folder.
    4. Update the Build Action: Make sure the "Copy Local" property for each reference is set to "True" to ensure the libraries are copied to the output directory during the build process.
    5. Ensure Compatibility: Test the project to make sure it works correctly. When distributing, include the "Lib" folder along with your project to ensure it runs on other computers.

    This approach keeps your libraries organized and ensures the project remains portable.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.