Using libraries in C# (Visual Studio) [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking us to recommend or find a tool, library or favorite off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 9 years ago.
Improve this question
I've been trying to download and use some libraries in my C# project but haven't succeed adding them to the project nor finding anywhere how to do it. The instructions says "simply add the files in the sourcecode folder into your project, compile and have a tea!" (the library is this by the way: http://spreadsheetlight.com/). But I don't think I compiled it properly and it's unable to find the functions/methods in the library and I get an error. Some help would therefore be deeply appreciated!
Thanks in advance!
Axel

I clicked your link, and downloaded whatever it was. :) It came as a zip folder. Obviously the first thing you need to do, is unzip your folder. Then you should end up with this:
The important thing here, is the SpreadsheetLight.dll...
Next, go to your project that you want this in, right click on the references folder and click Add reference:
A new window should pop up. Click on the browse tab, find your extracted folder, and select the SpreadsheetLight.dll file:
Choose OK.
Then view your code, find where all the imports are (i.e. using System;), and insert the following:
using SpreadsheetLight;
or go further to:
using SpreadsheetLight.Charts;
You can then access members of the library:
That is how you would generally use a library in Visual Studio... Good Luck!

I won't go into using NuGet, it's easy and there are more than enough examples / tutorials online for that. I'll explain the non NuGet way of manually referencing external libraries.
The easy way:
extract the dll files to your project folder
add references to the project by right-clicking References > Add Reference > browse to the dll location (inside your project folder) and add them
in files that reference namespaces, classes, interfaces, ... of these dlls, you'll need to add a using statement for those namespaces.
The better way:
make a folder somewhere (on a data disk/partition, in the cloud, ...) called e.g. Libraries where you can subfolder and extract your libraries to.
add references and possibly using statements as mentioned under the easy way.
set Copy Local to true for each referenced dll. This will copy the dll to the project/bin folder upon build. Please note that there are situations where this would not work (e.g. if the dll was already in the GAC) but for what you're trying to do, it should suffice.
This does not take into account whether the library is already registered in the GAC, if it's an ActiveX lib or any number of exceptions. This is just a way to simply add a library to a Visual Studio project.

Related

Which files in bin/Release folder are needed? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
After building the release version for my application, I went to the bin/Release folder and saw a bunch of my project dependencies, such as "Newtonsoft.Json.dll", "System.Memory.dll" and so on.
I have referenced posts like What files are mandatory in release windows form? and saw that these dependencies are needed for my application to run smoothly.
However, I only needed to copy one file - my "main.dll" to the application (I'm creating an add-on for Revit) and it works fine. So, I'm wondering which files could be excluded? This is because my application is still being modified and new dependencies are being added, and I would like to know which files are not needed.
Is there a list of dependencies that have already been included in windows automatically?
In short, you probably need them all and sometimes some. The long version -- it depends. Below is not an exhaustive list but some points to consider:
Whatever is part of the .NET framework that you are using is not normally copied to the output directory
The examples that you listed normally should be copied to the output as they are not part of .NET Framework
You can force a reference to be copied by setting Copy Local to true, which would result in a potentially unnecessary DLL in your output directory
You may have some dependencies in the GAC, which means they might not be copied to the output directory. For example, are you using Infragistics, etc. controls -- do all of your users have them installed on their machines? Probably not and they probably shouldn't, so include them...
The compiler is generally smart about not including things to which you have no actual code references (this can lead to problems if there are dynamic invocations only)

Getting a list of all dependencies from a .NET Standard project's csproj

Ever since I've been using the (relatively) new .NET Standard Library project type in Visual Studio, I've been having some problems getting a complete set of DLL files that are required by my project.
The problem is usually limited to 3rd-party libraries which I reference as NuGet packages. I've noticed that these don't get copied to the output folder of my project when I build it. This didn't use to be the case in classic project types.
While I can appreciate the de-cluttering effect that this change has brought for .NET Standard projects, I'm now faced with a problem. I sometimes absolutely need to be able to get the entire list of all files that my project depends on!
I have several different cases, where I might require this list for one reason or another, but the one I believe is most crucial for me, is when I want to gather these files from the csproj itself, right after it's built. In there, I have a custom MSBuild <Target> which should take all the files from the output dir and zip them together for distribution. The problem is, I'm missing all the files that come from NuGet dependencies, because they're not there!
How can I solve this in a general (i.e. not project-specific) way?
UPDATE
There's this deps.json file that contains basically all I'm after and then some. It's just a matter of extracting the relevant information and find the files in the local NuGet cache. But that would involve writing a specialized app and calling it from my target. Before I start writing one myself... Is there something like this already out there somewhere?
I followed this answer and it sort of works.
The suggested thing was to include the following into my csproj:
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
My main concern is that it also outputs some other DLLs from the framework (such as System.Memory.dll and System.Buffers.dll, among others), which I didn't expect. But maybe that's a good thing. They do seem to be dependencies, just not direct ones. I'll see how it plays out.
If it turns out ok, my only wish would be that this directive was more prominently displayed in project settings (as a simple checkbox, maybe?) so I wouldn't have to hunt the web to find it.

Link compile-time created resources to a class library

I'm using Visual Studio Community 2017 and my projects are written in C#.
Project setup:
Let's assume I have a class library project, called (A).
And I have another project that creates an executable, called (B).
In Visual Studio (B) has a reference on (A). Library (A)'s dll gets automatically copied over to the output directory of (B).
Problem:
The class library (A) needs some additional files, that are created while the project compiles.
(So I can't just add them to visual studio as resources and set them up as "copy if newer")
Project (A) has a pre-build command set up to create these resources.
This command invokes a content processor to process a bunch of library related assets.
I want these processed asset files to be linked to my library (A).
So if another project references (A), I want these asset files to get copied over as well.
Copying the processed assets into the output directory of (A) apparently did not do the trick.
My actual question is:
How do I tell Visual Studio to also copy the compile-time created files of project (A)
into project (B)'s output directory?
I know it looks kinda stupid to answer my own question like an hour later but...
Maybe someone else might find my solution useful:
As Alexan commented on my initial question, creating a custom nuget package and hosting it locally might be a relatively easy and elegant solution. I will definetly try that out as well. I think by following this blog post I might be able to achieve this:
https://www.wiliam.com.au/wiliam-blog/creating-a-nuget-package
But, I also figured out another way, which relates to this question:
How do I “Add Existing Item” an entire directory structure in Visual Studio?
As it turns out, by manually editing the csproj file of my class library, I can define a directory to include as linked content. All the files within this directory get fetched automatically when the projects gets compiled. All the files within this directory are copied over to project (B) automatically. (My compile-time generated assets as well)
<ItemGroup>
<Content Include="$([System.IO.Directory]::GetFiles("PathToFolderToInclude","*",SearchOption.AllDirectories))">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
So, I would call this topic solved. If somebody else has another idea how to solve this problem I would like to know.

How to manually add a library to a visual studio 2017 project?

My apologies for this very basic question that has assuredly been asked and answered before, also for my very dated terminology. I searched for an answer, but failed to find anything helpful. I am using the latest update of VS 2017 Community, and I'm trying to manually add what I used to call a "function library" to a project, which sits under a "solution".
The project is simply a C++ or C# console application. What I have is a .h (header) file, a .lib (static library) file, and a .dll (dynamic library) file. I intend to make "function calls" to this library. I'm aware that I need to have my dll in the debug folder where the executable resides, but I'm not sure how to "add dependencies" in VS 2017. I know how to manage and install NuGet packages, but these files aren't a handy-dandy NuGet package.
I hope to get advice on doing this the right (VS 2017) way.
What I do in a situation like this is to create a folder, I use C:\Etc\SDKs\<name_of_library> and then within that folder create an include subfolder, and a lib subfolder. Note that the top level folder choice is completely arbitrary, place it where it makes the most sense to you.
In the C/C++ section of project properties on the General tab, or the corresponding section for C# projects, there's an entry for Additional include directories. Add the path to your newly created include folder there. That'll let you include the header file and have it work right.
In the Linker section of project properties, also on its General tab, there's a corresponding entry for Additional library directories. Add the path to your lib folder there. On the next tab down: Input there's an entry for Additional Dependencies. Add the actual name of the library file there.
Those steps should allow your project to be built using the .h, .lib and .dll files you have.
-- Edit to address comments --
The .lib file does go in the ...\lib folder, and the .h file in the ...\include, that's correct. However, you had the location of the .dll correct in your original question. That needs to be somewhere on the search path that the executable will find, so the easiest place is the same folder as the executable.
General tab is a poor choice of words on my part. General section might have been better here. When looking at the project properties, the left most pane is a tree view of the various property sections. With everything closed up, except the very top item open, you'll see
Configuration Properties
General
Debugging
VC Directories
> C/C++
> Linker
...
If you then double click on C/C++ it'll open up, and show the sections specific to the C/C++ compiler:
Configuration Properties
General
Debugging
VC Directories
V C/C++
General <<<<<
Optimization
Preprocessor
...
> Linker
...
If you click on the word `General that I've highlighted, that'll get you to the General section / tab I was talking about.
Likewise, double clicking the word Linker will open up that section, and under that you'll find the Linker General and Input sections.
Let me know if this still isn't clear, and I'll try to clarify.

Syntax highlighting for non-project files in Visual Studio

I have a simple C# project which loads external C# files at startup to be used as scripts. Unfortunately when editing any of these 'non-project' files in Visual Studio I only get the most basic of syntax highlighting, since classes and types within the project are not known in the context of this external file.
Without adding the files to my project (defeating the purpose of them being external scripts), is there any way I can define an external interface or somehow otherwise convince Visual Studio (2008) to parse the code within these files in the context of the classes in the project?
A couple of clarifications (with thanks to the early answerers)
People should be able to edit these scripts without access to my source code
People shouldn't have to set up an entire Visual Studio project to edit one source file that's likely to contain less that 10 lines of actual code.
You will always need a reference to these classes. Maybe you can add these files as a link to the project or to a new project with a reference.
Visualstudio needs some informations to accomplish that.
I would think about the Bridge Pattern and you need to add the class body in the same file
http://en.wikipedia.org/wiki/Bridge_pattern
or using mock objects- you can easily use them to provide syntax highlighting without sharing your code (the same here - all in one file):
http://en.wikipedia.org/wiki/Mock_object
You can separate the script and the assiting classes if you would allow having a project file.

Categories

Resources