|
1 | 1 | --- |
2 | | -title: "Quickstart: Install and use a NuGet package in Visual Studio (Windows only)" |
3 | | -description: In this quickstart, you learn how to install and use a NuGet package in a Visual Studio project for Windows. |
| 2 | +title: "Quickstart: Install and Use a NuGet Package in Visual Studio (Windows Only)" |
| 3 | +description: In this quickstart, find out how to install and use the Newtonsoft.Json NuGet package in a Visual Studio project for Windows. |
4 | 4 | author: JonDouglas |
5 | 5 | ms.author: jodou |
6 | | -ms.date: 03/03/2025 |
| 6 | +ms.date: 04/16/2026 |
7 | 7 | ms.topic: quickstart |
| 8 | +# customer intent: As a developer, I want to find out how to install and use a NuGet package in a Visual Studio project so that I can take advantage of available code. |
8 | 9 | --- |
9 | 10 |
|
10 | 11 | # Quickstart: Install and use a NuGet package in Visual Studio (Windows only) |
11 | 12 |
|
12 | | -A *NuGet package* contains reusable code that other developers have made available to you for use in your projects. You can install a NuGet package in a Microsoft Visual Studio project by using the [NuGet Package Manager](../consume-packages/install-use-packages-visual-studio.md), the [Package Manager Console](../consume-packages/install-use-packages-powershell.md), or the [.NET CLI](install-and-use-a-package-using-the-dotnet-cli.md). This article demonstrates how to create a Windows Presentation Foundation (WPF) project with the popular `Newtonsoft.Json` package. The same process applies to any other .NET or .NET Core project. |
| 13 | +In this quickstart, you use Microsoft Visual Studio to install and use a *NuGet package* in a project. A NuGet package contains reusable code that other developers make available to you for use in your projects. |
13 | 14 |
|
14 | | -After you install a NuGet package, you can then make a reference to it in your code with the `using <namespace>` statement, where \<namespace\> is the name of package you're using. After you've made a reference, you can then call the package through its API. |
| 15 | +You can install a NuGet package in a Visual Studio project by using the [NuGet Package Manager](../consume-packages/install-use-packages-visual-studio.md), the [Package Manager Console](../consume-packages/install-use-packages-powershell.md), or the [.NET command-line interface (CLI)](install-and-use-a-package-using-the-dotnet-cli.md). This quickstart demonstrates how to use the NuGet Package Manager and the Package Manager Console to install a package. You follow steps for creating a Windows Presentation Foundation (WPF) project that uses the popular `Newtonsoft.Json` package. The same process applies to any other .NET project. |
15 | 16 |
|
16 | | -The article is for Windows users only. If you're using Visual Studio for Mac, see [Install and use a package in Visual Studio for Mac](install-and-use-a-package-in-visual-studio-mac.md). |
| 17 | +This article is for Windows users only. If you're using Visual Studio for Mac, see [Install and use a package in Visual Studio for Mac](install-and-use-a-package-in-visual-studio-mac.md). |
17 | 18 |
|
18 | 19 | > [!TIP] |
19 | | -> To find a NuGet package, start with *nuget.org*. Browsing nuget.org is how .NET developers typically find components they can reuse in their own applications. You can do a search of nuget.org directly or find and install packages within Visual Studio as shown in this article. For more information, see [Find and evaluate NuGet packages](../consume-packages/finding-and-choosing-packages.md). |
| 20 | +> To find a NuGet package, start with [nuget.org](https://www.nuget.org/). Browsing nuget.org is how .NET developers typically find components they can reuse in their own applications. You can do a search of nuget.org directly or find and install packages within Visual Studio as shown in this article. For more information, see [Find and evaluate NuGet packages for your project](../consume-packages/Finding-and-Choosing-Packages.md). |
20 | 21 |
|
21 | 22 | ## Prerequisites |
22 | 23 |
|
23 | | -- Install Visual Studio 2022 for Windows with the .NET desktop development workload. |
| 24 | +- Install Visual Studio 2026 with the .NET desktop development workload. |
24 | 25 |
|
25 | | - You can install the 2022 Community edition for free from [visualstudio.microsoft.com](https://visualstudio.microsoft.com/), or use the Professional or Enterprise edition. |
| 26 | + You can install the 2026 Community edition for free from [visualstudio.microsoft.com](https://visualstudio.microsoft.com/), or you can use the Professional or Enterprise edition. |
26 | 27 |
|
27 | 28 | ## Create a project |
28 | 29 |
|
29 | | -You can install a NuGet package into any .NET project if that package supports the same target framework as the project. However, for this quickstart you'll create a Windows Presentation Foundation (WPF) Application project. |
| 30 | +You can install a NuGet package into any .NET project if that package supports the same target framework as the project. For this quickstart, you create a WPF Application project. |
30 | 31 |
|
31 | 32 | Follow these steps: |
32 | 33 |
|
33 | | -1. In Visual Studio, select **File** > **New** > **Project**. |
| 34 | +1. In Visual Studio, select **File** > **New** > **Project/Solution**. |
34 | 35 |
|
35 | | -1. In the **Create a new project** window, enter *WPF* in the search box and select **C#** and **Windows** in the dropdown lists. In the resulting list of project templates, select **WPF Application**, and then select **Next**. |
| 36 | +1. In the **Create a new project** window, go to the search box and enter **wpf**. In the resulting list of project templates, select the **WPF Application** template that has **C#** and **Windows** tags, and then select **Next**. |
36 | 37 |
|
37 | | -1. In the **Configure your new project** window, optionally update the **Project name** and the **Solution name**, and then select **Next**. |
| 38 | +1. In the **Configure your new project** window, optionally update the **Project name** and **Solution name** values, and then select **Next**. |
38 | 39 |
|
39 | | -1. In the **Additional information** window, select **.NET 6.0** (or the latest version) for **Framework**, and then select **Create**. |
| 40 | +1. In the **Additional information** window, under **Framework**, select **.NET 10.0** (or the latest version), and then select **Create**. |
40 | 41 |
|
41 | 42 | Visual Studio creates the project, and it appears in [Solution Explorer](/visualstudio/ide/use-solution-explorer). |
42 | 43 |
|
43 | 44 | ## Add the Newtonsoft.Json NuGet package |
44 | 45 |
|
45 | | -To install a NuGet package in this quickstart, you can use either the NuGet Package Manager or the Package Manager Console. Depending on your project format, the installation of a NuGet package records the dependency in either your project file or a *packages.config* file. For more information, see [Package consumption workflow](../consume-packages/overview-and-workflow.md). |
| 46 | +To install a NuGet package in this quickstart, you can use either the NuGet Package Manager or the Package Manager Console. Depending on your project format, the installation of a NuGet package records the dependency in either your project file or a *packages.config* file. For more information, see [Package consumption workflow](../consume-packages/Overview-and-Workflow.md). |
46 | 47 |
|
47 | 48 | ### NuGet Package Manager |
48 | 49 |
|
49 | 50 | To use the [NuGet Package Manager](../consume-packages/install-use-packages-visual-studio.md) to install the `Newtonsoft.Json` package in Visual Studio, follow these steps: |
50 | 51 |
|
51 | 52 | 1. Select **Project** > **Manage NuGet Packages**. |
52 | 53 |
|
53 | | -1. In the **NuGet Package Manager** page, choose **nuget.org** as the **Package source**. |
| 54 | +1. On the **NuGet Package Manager** page, next to **Package source**, select **nuget.org**. |
54 | 55 |
|
55 | | -1. From the **Browse** tab, search for *Newtonsoft.Json*, select **Newtonsoft.Json** in the list, and then select **Install**. |
| 56 | +1. Go to the **Browse** tab and search for **Newtonsoft.Json**. In the list, select **Newtonsoft.Json**, and then select **Install**. |
56 | 57 |
|
57 | | - :::image type="content" source="media/qs-use-install-package.png" alt-text="Screenshot showing the NuGet Package Manager window with the Newtonsoft.Json package selected."::: |
| 58 | + :::image type="content" source="media/qs-use-install-package.png" alt-text="Screenshot of the NuGet Package Manager. The Newtonsoft.Json package is selected. Its details pane displays package data and has an Install button." lightbox="media/qs-use-install-package.png"::: |
58 | 59 |
|
59 | | -1. If you're prompted to verify the installation, select **OK**. |
| 60 | +1. If you're prompted to verify the installation, select **Apply**. |
60 | 61 |
|
61 | 62 | ### Package Manager Console |
62 | 63 |
|
63 | 64 | Alternatively, to use the [Package Manager Console](../consume-packages/install-use-packages-powershell.md) in Visual Studio to install the `Newtonsoft.Json` package, follow these steps: |
64 | 65 |
|
65 | | -1. From Visual Studio, select **Tools** > **NuGet Package Manager** > **Package Manager Console**. |
| 66 | +1. In Visual Studio, select **Tools** > **NuGet Package Manager** > **Package Manager Console**. |
66 | 67 |
|
67 | | -1. After the **Package Manager Console** pane opens, verify that the **Default project** drop-down list shows the project in which you want to install the package. If you have a single project in the solution, it's preselected. |
| 68 | +1. At the top of the **Package Manager Console** window, verify that the **Default project** list shows the project in which you want to install the package. If you have a single project in the solution, it's preselected. |
68 | 69 |
|
69 | | - :::image type="content" source="media/qs-use-package-manager-console.png" alt-text="Screenshot showing the Package Manage Console window with Default project highlighted."::: |
| 70 | + :::image type="content" source="media/qs-use-package-manager-console.png" alt-text="Screenshot of the Package Manager Console window, which contains information about the version and licensing. The Default project list is highlighted." lightbox="media/qs-use-package-manager-console.png"::: |
70 | 71 |
|
71 | 72 | 1. At the console prompt, enter the command `Install-Package Newtonsoft.Json`. For more information about this command, see [Install-Package](../reference/ps-reference/ps-ref-install-package.md). |
72 | 73 |
|
73 | 74 | The console window shows the output for the command. Errors typically indicate that the package isn't compatible with the project's target framework. |
74 | 75 |
|
75 | 76 | ## Use the Newtonsoft.Json API in the app |
76 | 77 |
|
77 | | -With the `Newtonsoft.Json` package in the project, call its `JsonConvert.SerializeObject` method to convert an object to a human-readable string: |
78 | | - |
79 | | -1. From **Solution Explorer**, open *MainWindow.xaml* and replace the existing `<Grid>` element with the following code: |
80 | | - |
81 | | - ```xaml |
82 | | - <Grid Background="White"> |
83 | | - <StackPanel VerticalAlignment="Center"> |
84 | | - <Button Click="Button_Click" Width="100px" HorizontalAlignment="Center" Content="Click Me" Margin="10"/> |
85 | | - <TextBlock Name="TextBlock" HorizontalAlignment="Center" Text="TextBlock" Margin="10"/> |
86 | | - </StackPanel> |
87 | | - </Grid> |
88 | | - ``` |
89 | | - |
90 | | -1. Open the *MainWindow.xaml.cs* file under the *MainWindow.xaml* node, and insert the following code inside the `MainWindow` class after the constructor: |
91 | | - |
92 | | - ```csharp |
93 | | - public class Account |
94 | | - { |
95 | | - public string Name { get; set; } |
96 | | - public string Email { get; set; } |
97 | | - public DateTime DOB { get; set; } |
98 | | - } |
99 | | - |
100 | | - private void Button_Click(object sender, RoutedEventArgs e) |
101 | | - { |
102 | | - Account account = new Account |
103 | | - { |
104 | | - Name = "John Doe", |
105 | | - |
106 | | - DOB = new DateTime(1980, 2, 20, 0, 0, 0, DateTimeKind.Utc), |
107 | | - }; |
108 | | - string json = JsonConvert.SerializeObject(account, Newtonsoft.Json.Formatting.Indented); |
109 | | - TextBlock.Text = json; |
110 | | - } |
111 | | - ``` |
112 | | - |
113 | | -1. To avoid an error for the `JsonConvert` object in the code (a red squiggle line will appear), add the following statement at the beginning of the code file: |
114 | | - |
115 | | - ```csharp |
116 | | - using Newtonsoft.Json; |
117 | | - ``` |
118 | | - |
119 | | -1. To build and run the app, press F5 or select **Debug** > **Start Debugging**. |
| 78 | +After you install a NuGet package, you can make a reference to it in your code by using the `using <namespace>` statement, where \<namespace\> is the name of the package you're using. After you make a reference, you can call the package through its API. |
120 | 79 |
|
121 | | - The following window appears: |
| 80 | +With the `Newtonsoft.Json` package in the project, you can call its `JsonConvert.SerializeObject` method. To use this method to convert an object to a human-readable string, take these steps: |
122 | 81 |
|
123 | | -  |
| 82 | +1. In **Solution Explorer**, open *MainWindow.xaml* and replace the existing `<Grid>` element with the following code: |
124 | 83 |
|
125 | | -1. Select the **Click Me** button to see the contents of the `TextBlock` object replaced with JSON text. |
| 84 | + ```xaml |
| 85 | + <Grid Background="White"> |
| 86 | + <StackPanel VerticalAlignment="Center"> |
| 87 | + <Button Click="Button_Click" Width="100px" HorizontalAlignment="Center" Content="Click Me" Margin="10"/> |
| 88 | + <TextBlock Name="TextBlock" HorizontalAlignment="Center" Text="TextBlock" Margin="10"/> |
| 89 | + </StackPanel> |
| 90 | + </Grid> |
| 91 | + ``` |
126 | 92 |
|
127 | | -  |
| 93 | +1. In **Solution Explorer**, expand the **MainWindow.xaml** node, and then open the *MainWindow.xaml.cs* file. Insert the following code inside the `MainWindow` class, after the constructor: |
128 | 94 |
|
129 | | -## Related video |
| 95 | + ```csharp |
| 96 | + public class Account |
| 97 | + { |
| 98 | + public string ID { get; set; } |
| 99 | + public decimal Balance { get; set; } |
| 100 | + public DateTime Created { get; set; } |
| 101 | + } |
130 | 102 |
|
131 | | -- Find NuGet videos on [Channel 9](/shows/nuget-101/) and [YouTube](https://www.youtube.com/playlist?list=PLdo4fOcmZ0oVLvfkFk8O9h6v2Dcdh2bh_). |
| 103 | + private void Button_Click(object sender, RoutedEventArgs e) |
| 104 | + { |
| 105 | + Account account = new Account |
| 106 | + { |
| 107 | + ID = "A1bC2dE3fH4iJ5kL6mN7oP8qR9sT0u", |
| 108 | + Balance = 4389.21m, |
| 109 | + Created = new DateTime(2026, 4, 16, 0, 0, 0, DateTimeKind.Utc), |
| 110 | + }; |
| 111 | + string json = JsonConvert.SerializeObject(account, Newtonsoft.Json.Formatting.Indented); |
| 112 | + TextBlock.Text = json; |
| 113 | + } |
| 114 | + ``` |
132 | 115 |
|
133 | | -## See also |
| 116 | +1. If *MainWindow.xaml.cs* doesn't contain the following line, add it to the beginning of the file: |
134 | 117 |
|
135 | | -For more information about NuGet, see the following articles: |
| 118 | + ```csharp |
| 119 | + using Newtonsoft.Json; |
| 120 | + ``` |
136 | 121 |
|
137 | | -- [What is NuGet?](../what-is-nuget.md) |
138 | | -- [Package consumption workflow](../consume-packages/overview-and-workflow.md) |
139 | | -- [Find and choose packages](../consume-packages/finding-and-choosing-packages.md) |
140 | | -- [Package references in project files](../consume-packages/package-references-in-project-files.md) |
141 | | -- [Install and use a package using the .NET CLI](install-and-use-a-package-using-the-dotnet-cli.md). |
142 | | -- [Newtonsoft.Json package](https://www.nuget.org/packages/newtonsoft.json) |
| 122 | + Without this line, Visual Studio marks the `JsonConvert` object with a red, squiggly line to indicate an error. |
| 123 | + |
| 124 | +1. To build and run the app, select **F5**, or select **Debug** > **Start Debugging**. |
| 125 | + |
| 126 | + The following window appears: |
| 127 | + |
| 128 | + :::image type="content" source="media/qs-use-wpf-app-start.png" alt-text="Screenshot of the MainWindow window that the app creates in Visual Studio. The window contains a Click Me button and the term TextBlock." lightbox="media/qs-use-wpf-app-start.png"::: |
143 | 129 |
|
144 | | -## Next steps |
| 130 | +1. Select **Click Me**. The app updates the window by replacing the `TextBlock` object with JSON text. |
145 | 131 |
|
146 | | -Congratulations on installing and using your first NuGet package. Advance to the next article to learn more about installing and managing NuGet packages. |
| 132 | + :::image type="content" source="media/qs-use-wpf-app-end.png" alt-text="Screenshot of the MainWindow window in Visual Studio, which contains a Click Me button and JSON code that lists ID, Balance, and Created values." lightbox="media/qs-use-wpf-app-end.png"::: |
147 | 133 |
|
148 | | -> [!div class="nextstepaction"] |
149 | | -> [Install and manage packages using using the NuGet Package Manager](../consume-packages/install-use-packages-visual-studio.md) |
| 134 | +## Related videos |
150 | 135 |
|
151 | | -> [!div class="nextstepaction"] |
152 | | -> [Install and manage packages using the Package Manager Console](../consume-packages/install-use-packages-powershell.md) |
| 136 | +For videos about using NuGet for package management, see [.NET Package Management with NuGet for Beginners](/shows/dotnet-package-management-with-nuget-for-beginners/) and [NuGet for Beginners](https://www.youtube.com/playlist?list=PLdo4fOcmZ0oVLvfkFk8O9h6v2Dcdh2bh_). |
| 137 | + |
| 138 | +## Related content |
| 139 | + |
| 140 | +To find out more about installing and managing NuGet packages, see the following articles: |
| 141 | + |
| 142 | +- [Install and manage packages in Visual Studio using the NuGet Package Manager](../consume-packages/install-use-packages-visual-studio.md) |
| 143 | +- [Manage packages with the Visual Studio Package Manager Console (PowerShell)](../consume-packages/install-use-packages-powershell.md) |
| 144 | + |
| 145 | +For more information about NuGet, see the following articles: |
| 146 | + |
| 147 | +- [An introduction to NuGet](../what-is-nuget.md) |
| 148 | +- [Package consumption workflow](../consume-packages/Overview-and-Workflow.md) |
| 149 | +- [Find and evaluate NuGet packages for your project](../consume-packages/Finding-and-Choosing-Packages.md) |
| 150 | +- [`PackageReference` in project files](../consume-packages/Package-References-in-Project-Files.md) |
| 151 | +- [Install and use a package with the dotnet CLI](install-and-use-a-package-using-the-dotnet-cli.md) |
| 152 | +- [Newtonsoft.Json package](https://www.nuget.org/packages/newtonsoft.json) |
0 commit comments