Getting Started with .NET Core on Windows
This tutorial will guide you through setting up your environment and creating your first .NET Core application on Windows. We'll cover installation, basic project structure, and running your application.
1. Prerequisites
- Windows 10 or later
- Visual Studio 2022 (Community, Professional, or Enterprise) or VS Code
- Internet connection for downloading .NET SDK
2. Installing the .NET SDK
Download the latest .NET SDK from the official dotnet.microsoft.com. Make sure to select the correct version for Windows (x64 or ARM64).
Follow the on-screen instructions for a standard installation.
3. Creating Your First .NET Core Application
You can create applications using either Visual Studio or the .NET CLI.
Using Visual Studio:
- Open Visual Studio.
- Click "Create a new project".
- Search for "Console App" and select the C# template.
- Click "Next".
- Enter your project name (e.g.,
MyFirstWindowsApp) and choose a location. - Select the desired .NET version (e.g., .NET 8.0).
- Click "Create".
Using the .NET CLI:
Open a command prompt or PowerShell and run the following commands:
dotnet new console -o MyFirstWindowsApp
cd MyFirstWindowsApp
4. Exploring the Project Structure
Your project will typically contain:
.csprojfile: The project file containing build information.Program.cs: The entry point of your application.objandbinfolders: Created during the build process.
5. Running Your Application
In Visual Studio, press F5 or click the "Run" button.
Using the .NET CLI, run:
dotnet run
You should see the output:
Hello, World!
Congratulations! You've successfully created and run your first .NET Core application on Windows.
Next: Building a Desktop ApplicationBuilding a Windows Desktop Application with WPF
Learn how to create native Windows desktop applications using Windows Presentation Foundation (WPF) and .NET Core.
1. Creating a WPF Project
Using Visual Studio, create a new project and search for "WPF App".
dotnet new wpf -o MyWpfApp
cd MyWpfApp
2. Understanding WPF Concepts
- XAML: The declarative language for defining the UI.
- Code-Behind: C# code for handling UI logic and events.
- Data Binding: Connecting UI elements to data sources.
- Styles and Templates: Customizing the appearance and behavior of controls.
3. Designing Your UI
Edit the MainWindow.xaml file to define your window's layout and controls.
<Window x:Class="MyWpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MyWpfApp"
mc:Ignorable="d"
Title="My WPF App" Height="450" Width="800" WindowStartupLocation="CenterScreen">
<Grid Margin="10">
<TextBlock Text="Welcome to WPF on .NET Core!"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="24"
Foreground="{StaticResource PrimaryBrush}"/>
</Grid>
</Window>
You'll need to define PrimaryBrush in your App.xaml or resources.
4. Adding Interactivity
Modify MainWindow.xaml.cs to handle events and logic.
// MainWindow.xaml.cs
using System.Windows;
namespace MyWpfApp
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
5. Building and Running
Build your project in Visual Studio or use the CLI:
dotnet build
dotnet run
Explore the official Microsoft Docs for Windows Desktop for more advanced topics.
Next: Deploying Your ApplicationDeployment Options for .NET Core on Windows
Understand how to deploy your .NET Core applications to Windows users.
1. Framework-Dependent Deployment (FDD)
- The .NET runtime is installed on the target machine.
- Your application is smaller as it doesn't include the runtime.
- Requires the target machine to have a compatible .NET SDK or Runtime installed.
To publish:
dotnet publish -c Release -f net8.0-windows --runtime win-x64 --self-contained false
2. Self-Contained Deployment (SCD)
- Includes the .NET runtime with your application.
- Your application can run on machines without a pre-installed .NET runtime.
- Results in larger deployment packages.
To publish:
dotnet publish -c Release -f net8.0-windows --runtime win-x64 --self-contained true
3. MSI Installer
For a professional installation experience, create an MSI installer using tools like:
- WiX Toolset
- Visual Studio Installer Projects Extension
This allows for standard Windows installation, uninstallation, and updates.
4. ClickOnce Deployment
A simpler deployment technology for applications deployed over a network (HTTP/FTP) or from removable media. Supports automatic updates.
5. Packaging for Microsoft Store
Package your application using MSIX for distribution through the Microsoft Store, offering modern deployment and update capabilities.
For more details, visit the official .NET Deployment documentation.