How to use NuGet? - c#

I have installed NuGet, how to use it?
I saw the video and i tried
>Add-Package log4j
Command "Add" is not valid.
>
it does not work, I entered that in Command Window.
EDIT:
I uses VS2010 and I build WindowsForm App and not ASP.NET.

You've got two options;
You can either use the GUI; right click on project references > Add Library Package Reference...
Or you can use the console, which has more features, but I'd not recommend it.

You're entering this in the Command window when you should be entering it in the Package Manager Console window. Open it through 'View' / 'Other windows' / 'Package Manager Console'.

To answer your specific question besides the documentation link:
Using the Package Manager Console you can find a package by filtering it i.e.:
get-package -remote -filter Log4net
Once you find the package you want, you can add it to your project with
install-package log4net
That's all there is to it - if you don't need to use the console then for casual use the Add Library Package Reference Dialog Box (that's a handful) provides an easy to use UI to add packages.

Click on project --> Manage nuget packages.
You can see this youtube video which shows the importance of Nuget and how to use by using VS 2012
http://www.youtube.com/watch?v=AXfZsTj-7P0

If you have visual studio 2013 or above.. the solutions now come up with Nuget package manager and a packages.config file is also added to the solution. packages.config file contains information about all the packages present in the solution.
Read more > About Nuget Install/Uninstall & Downgrading

Related

How to achieve full NuGet reinstall for dozens of solutions?

We have dozens of solutions in a repository and we're retargeting every project to net472 from net462. Currently our best bet is to open each and every solution in Visual Studio and execute the following command in the Package Manager Console.
Update-Package -Reinstall -IgnoreDependencies
As far as I'm aware, the PM console cannot be used outside Visual Studio, so this method of course is not very efficient, so what I was thinking about is using the nuget.exe tool for this. However at first glance I could not find any equivalent operation or argument set.
The documentation at this moment says the following
For all packages, delete the package folder, then run nuget install.
For a single package, delete the package folder and use nuget install
to reinstall the same one.
So based on this I tried to delete the packages folder and run nuget install for a project, so I expected it to do a re-install. However, while it installed the package indeed (to packages), it does not touch the packages.config (for retargeting).
Is anyone aware of any kind of possible way to automate this process?
How to achieve full NuGet reinstall using nuget.exe CLI?
That command cannot get what you want.
As far as I know, nuget install should be with packages.config file and it will not update the nuget framework version of packages.config file automatically.
So whenever you change the target framework version of your project, using that command will not update the target framework version of the nuget package.
So only update-package -reinstall command under Package Manager Console will update the target framework version of packages.config file.
And also Package Manager Console cannot access multiple solutions so you have to open each solution to run that command.
Although it may be possible to achieve your expectations with PowerShell scripts, but it is too complex so that it is easier to open each solution and then run the command.
As a suggestion,
1) open each solution on VS to run update-package -reinstall command.
2) And migrating from packages.config to PackageReference may be a good choice. In this case, the nuget packages will automatically adapt to the corresponding project target framework version.
Before doing this,you can make a backup of your project.
3) If these all do not meet your requirements, you could suggest a feature on our User Voice Forum to report your desire for automation. After that, you can share the link here and anyone who is interested in it will vote it so that it will get more attention from Microsoft.

File is not accessible - Visual Studio 2017

I have downloaded Visual Studio 2017/Community, for test automation, but when I added the reference selenium-dotnet 3.14.0 , it's saying that this file is not accessible.
Please help. Thanks
click here to see the problem
Have you tried going to the NuGet Package Manager?
Go to Tools -> NuGet Package Manager -> Manage NuGet Packages for Solution
Then Search for Selenium and v3.14.0 is at the top
You can also do this from the NuGet Package Manager Console with:
Install-Package Selenium.WebDriver
According to the screenshot, you're trying to add a NuGet package through the "Add reference" dialog, which is designed to add references to DLLs directly. NuGet packages typically contain DLLs but you have to install them using the Manage NuGet Packages for Solution dialog as #ZP123 explained and not through Add Reference.

Adding a NuGet package to a project without Visual Studio

I'm trying to automate adding NuGet packages to project on a remote server that doesn't have Visual Studio (nor any build servers) installed. I do know how to use NuGet.exe from my C# code, but this executable only downloads packages and doesn't do any other required work (adding references, executing ps scripts etc). What do I do in order to fully install a package?
Note: I don't need to update a package for my own app, I need to add a package to an arbitrary .csproj file on the server. I'm building a Web-based .Net IDE, and need my users to be able to add packages to their projects.
I see two possibilities: one is using some kind of functionality not present in NuGet.exe, but ratherin some other library (maybe a VS addin), but I don't know where to look for it. The other is to simulate some kind of NuGet Powershell console and send commands to it, but again, I don't know how to do that.
You might want to consider SharpDevelop.
Installing NuGet Packages outside of Visual Studio an article about the functionality you are trying to achieve (written by Matt Ward, one of the project contributors) says:
Since NuGet uses PowerShell the simplest approach was to extend the
existing PowerShell cmdlets included with SharpDevelop. Now you can
write a few lines of PowerShell script to install a NuGet package into
a project that has never had a NuGet package before, have the project
itself updated and any package PowerShell scripts run. All this from
the command line without Visual Studio open.
Just to add an additional answer in-line with Alex's post about SharpDevelop, you have a couple different options.
Use Nuget.exe (Related Blog post)
NuGet Addin for MonoDevelop and Xamarin Studio (see GitHub)
ASP.NET Pages using WebMatrix (see this video)
All of this info is from the Nuget FAQ.

how to add assembly DocumentFormat.OpenXml.Wordprocessin in visual studio

I want to read a doc file and do some operation and I found a code, but it has asking following "WordprocessingDocument" namespace.Has anyone know from where I can find this assembly.please dont sent me a download link for .dll file because it not good to add third party .dll directly.
you can download "Open XML SDK 2.5 for Microsoft Office" from http://www.microsoft.com/en-us/download/details.aspx?id=30425
for using this lib go to http://msdn.microsoft.com/en-us/library/office/bb448854.aspx
As per recent updates and methods for adding any namespaces related to Open XML SDK please add name spaces like System.IO.Packaging, DocumentFormat.OpenXml etc via Nuget Package. You can find nuget package on https://www.nuget.org/packages/DocumentFormat.OpenXml/
To Install this Nuget Package you can open Nuget Package Mananger console (Tools -> Nuget Package Manager -> Nuget Package Manager Console) and make sure that you have right project selected in console dropdown.
Run the following command.
Install-Package DocumentFormat.OpenXml
Alternatively you can also install it via Nuget Package Manager GUI which you can open simply by Right clicking on Solution Explorer and going in 'Manage Nuget Packages'

How to install Moq framework

I plan on using Moq to mock some interfaces in the unit test code I've created. I've already downloaded the latest version of Moq.
My question is how do I install it? Where should I place the Moq.dll?
I've tried searching on the internet, but all I can find are samples of how to use Moq, not how to install it.
The best way to add reference to Moq framework is installing it from Nuget. Also you still can download Moq.dll and add reference to this library (usually I create folder libs under the solution folder, where I put all third-party libraries, which is not available via Nuget).
BTW Another option to install package from Nuget - right click on project references and select Manage Nuget packages.... Then search online for Moq and install it. See why use Nuget over installing libraries directly on my machine
There's no need to install it. Just add a reference to the moq.dll in your project.
But of course you can use gacutil to register the library in your global assembly cache.
c:\path> gacutil /i Moq.dll
When using Visual Studio:
Right click on References [It's in the project Explorer]
Manage NuGet Packages
Search for Moq and add it to your solution.
This is an old question, but the convenient method I used is not listed here and this is the first result on google. I am using VS 2013 and if I search for Moq in Extensions and Updates there are no results so:
Go to Package Manager Console - (Tools -> Library Package Manager)
Change the default project to your test project
Then type: install-package moq
For .NET Core, using the dotnet CLI - dotnet add package Moq
You don't need to install it.
You could use NuGet of course (if you use newer versions of VS), but you can just copy it to your project folder (or preferably something like lib subdirectory of your project folder) and just add a reference to it.
EDIT:
You seem to have problem with wrong version. In your downloaded moq zip archive, there are multiple folders. You need to use one from folder Net35, not one from Net40. These numbers refer to the version of target .NET framework, not version of Moq itself.
When using Visual Studio
Right-click the test project in Solution Explorer
Manage NuGet packages...
Change Package source to All
Browse for Moq
Install
If you are on newer version of Visual Studio (2013+), you can use Package Manager Console.
Tools > Nuget Package Explorer > Package Manager Console
Execute:
Install-Package Moq -Version 4.5.16
Also see:
https://www.nuget.org/packages/Moq/

Categories