Reference C# code from another project without the .exe available? - c#

I have a solution with two projects:
Project A, a large private project
Project B, a small public project which needs a subset of the functions from A
I'm just getting started on Project B, but I already have a Project (A) which handles many things that will be needed in the new one. I would obviously like to reuse these functions without copying them.
If I add these as a reference in Visual Studio (2012) everything works code wise for Project B, but the generated assembly directory will also contain a functioning copy of Project A.exe.
I need to include my code from Project A without having a compiled .exe file in my output directory. Either by having it as a .dll file or inlined into the main assembly without having to refactor out the relevant parts of Project A into a new class library project. Is this possible?
Thanks for your time

I think you can try to change the Application Type of your "A Project" from Windows Application to Class Library
After that, compile your "A Project" and you will find the .dll in the output directory (instead the .exe)
Now that you have the .dll you can use it as a reference in your "B Project".
To change the Application Type you can visit this link form MSDN

Related

BLL and DLL are not founded, when I move them somewhere except source repos(Visual Studio 2019)

I have 3-layer MVVM project, my solution folder, DLL folder and BLL folder are inside 'source > repos >' and project loads normally
But if I move BLL or DLL(or both) to some other folder, or Desktop for example, my project doesn't load correctly and file moved to Desktop defined as not founded in solution explorer
How could I fix it? I need put all this three project folders(DLL, BLL, View) to one folder, because I need to share it as one big project, but I can't, because any movement with files or folder finishes with error 'Files not founded'
Sadly can not send images here, in y question(
Thanks in advance for your answers!
Check for that in your project: is it in the references list? If it is, check that it's built for the same CPU type as your project, and that the Reference properties option "Copy Local" is set to true.
If it isn't, try adding it. If that works, try rebuilding. If it doesn't, then it's probably a Native Library, and you need to ensure that the DLL is either in the EXE folder, or in a folder on the current PATH that Windows uses to locate executables.
You Need to compile the DLL project into a dll file and then refer to the compiled dll file.
Or just use project references to add your DLL project and BLL project.

How to share cs code among multiple solutions?

I'm using VS 2017 and I have a number of Utility class files (NetworkLib.cs, ImageLib.cs, etc) that I find myself constantly having to duplicate over and over again when creating new solutions.
Normally, I would just end up creating a dedicated separate solution, NetworkLib.csproj, compiling these into DLL files, and then adding a DLL reference whenever I need them in a new solution, but this isn't ideal for me.
I want to be able to use these in a shared library sense across multiple solution files but also when debugging if I step-into some function NetworkLib.Post() the debugger should step into the appropriate CS files. Additionally, if I make some changes to NetworkLib.cs from Solution1, all other solutions should pick it up on rebuilding.
I've given thought to creating a standalone Utility solution with a NetworkLib csproj, and then adding the project csproj as an existing project to each solution, but not sure if that's a good approach.
I also just read about the "Shared Project" but when I created it (in its own standalone solution) I can't seem to import it into other solutions. When I look in "Shared Projects" tab it just states "No Items Found".
Normally, I would just end up creating a dedicated separate solution,
NetworkLib.csproj, compiling these into DLL files, and then adding a
DLL reference whenever I need them in a new solution
That's the solution. You should publish your shared DLLs into a "shared" folder, somewhere on your desktop/server/network/... and reference these DLLs in your solutions. If you rebuild the shared library, every solution referencing it will automatically use the new version.
To debug external libs, you will need the .pdb file generated alongside with the .dll file. Look here for complete answer : How to debug external class library projects in visual studio?
Use "Linked items" - where the .csproj project has a soft reference to a *.cs file (or other file type) located anywhere in the filesystem, including outside of your source-control workspace or even a network share - just so long as the file exists your project will build. Use this technique judiciously because it's easy to break CI/CD systems for obvious reasons.
"How to: Add Existing Items to a Project": https://msdn.microsoft.com/en-us/library/9f4t9t92(v=vs.100).aspx

How to create two different executables from one Visual Studio project

I have a main executable that runs based on settings saved in a configuration file. I want to be able to change the settings in the config file through a different executable.
Is there an easy way of building these two different executables in one Windows Forms project? Meaning that when I press build, two different EXE files get created in the same solution folder - the one that changes the configuration file, and the other that uses it.
I know that this is possible to do if I create two separate projects that use the same solution folder, but I was hoping to do it all in one step.
I am assuming that to do this, I need a project with two "Main" functions. Is this possible?
You can build as many assemblies in one solution as you like. Assemblies can result in DLL files or EXE files.
Create a solution (or open an existing solution).
Right-click the root node in Solution Explorer and choose Add → New Project and choose the project type you like to add.
Right-click the project item in Solution Explorer and choose Properties → Build → Output path. Set to the desired directory where to build it to. Repeat this for the other projects.
This way you get the following in Solution Explorer:
MySolution
MyCommonCode (Class Library, results in MyCommonCode.dll)
MyMainApp (Windows Forms application, results in MyMainApp.exe)
MyConfigApp (Windows Forms application, results in MyConfigApp.exe)
The MyCommonCode assembly contains shared code that both EXE files are using like the identifiers of your configuration file, etc.
MyMainApp is the GUI application (Windows Forms, WPF, etc.) for your main application with a project-reference to the MyComonCode project.
MyConfigApp is a GUI application for editing the configuration values with a project reference to MyCommonCode project.
After building your solution you get the following binaries: MyCommonCode.dll, MyMainApp.exe, and MyConfigApp.exe.
Update based on the comment:
One compile-run can build only one binary (DLL or EXE) per project. You can do something like the answer above: move most of the code in a common/core DLL and make two thin projects for the two EXE files which only "configure and use" the central common/core DLL file.
You can build different EXE files based on the same project using compiler defines. You can even define your own defines. But per compile-run you can only build one binary (DLL, EXE) per project - one or the other, but not both.

Visual Studio 2013 C# configuration settings

I want to add include directories and libraries in my c# project like i can in my c++ project
C++ Project : There are options to include directories and linker dependencies.
C# Project
There does not appear to be an option in the project settings to add those settings. I added the path in reference path, but my debugger throws an error. Right now i've added all the dll in my project\bin\debug directory and its working, but i don't want to do it for all the projects. Where i can link these diretories ?
you can add reference of library files which you want to add into your project by right clicking the project under solution explorer and then "add reference" and then browse or select from the list of dlls..
You can't. You have to include references to code files and assemblies one by one.
The linker and compiler for .NET works different than you are used to with c++. That's why you can't just link an entire directory containing some code files / assemblies.
Add reference to your existing dll for every project which will be use it
http://msdn.microsoft.com/en-us/library/7314433t(v=vs.90).aspx
https://www.google.it/search?q=add+reference+c%23&es_sm=122&source=lnms&tbm=isch&sa=X&ei=P-2fU472Bcr00gX-tYAw&ved=0CAoQ_AUoAw&biw=1920&bih=955

Copy exe references to output folder

Say I create a visual studio class project A which is a wrapper around an exe. Getting the exe to be copied to the output directory is easy.
Now I create another project B that references the class A. Is there a way to set up project A such that project B will also copy the exe to the output directory when compiling? Like the exe to be a sort of "copy local dependency" of project A.
Thanks
Charles
Right, this is one way to do it. Will work if you have them both in the same solution in Visual Studio.
Add the reference Project A to Project B. (Or vice versa if I missunderstood you)
Right click the solution and select properties.
In Common properties you select Project dependencies.
In the project drop down select Project B and check Project A in "Depends on"-Checked listbox. (or vice versa)
Build project.
You should now have Project A.exe in Project B output folder.
To add an existing exe, which isn't inside your solution, you add the exe as a reference to your project.

Categories

Resources