Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
strauss_d_getting_started_with_visual_studio_2022_learning_a.pdf
Скачиваний:
123
Добавлен:
26.06.2023
Размер:
17.04 Mб
Скачать

Chapter 2 Working with Visual Studio 2022

Figure 2-6.  Filter project templates

You are, therefore, able to quickly find the most suitable project template.

Take note, though, that you may need to install a workload if you do not find the project template you want. To do this, click Install more tools and features. Refer to Chapter 1 to see how to use workloads in Visual Studio.

Of the several project templates available, let’s see which ones there are and what project is suitable for specific situations.

Various Project Templates

You can choose from a host of project templates in Visual Studio 2022. I would even go as far as to say that it’s now even easier to find the template you need to use due to the filters in the Create a new project window. Let’s have a look at a few of these project templates next.

69

Chapter 2 Working with Visual Studio 2022

Console Applications

I remember the first time I wrote a single line of code. I used a Console Application, a great template to use when you don’t need a UI for your application. The Console

Application project template running on the .NET Framework is displayed in Figure 2-7.

Figure 2-7.  Console App (.NET Framework)

You shall notice that this application is suited for running on Windows machines. But what if you need to run the Console Application across platforms such as Windows, Linux, and macOS? Here is where .NET 6 comes into play.

The Console Application project template running on .NET 6 is displayed in Figure 2-8.

Figure 2-8.  A .NET 6 Console App

A few years ago (long before .NET 6 was ever a thing), I needed to create an application that could be triggered on a schedule. The application’s executable would then be passed one of several parameters to determine which database to connect to.

The application had to run without any user intervention to perform some sort of maintenance task. Because no user intervention was needed, a Console Application best suited the use case. Be aware that a Console Application can accept user input, but for my purposes with this application, it was not necessary.

70

Chapter 2 Working with Visual Studio 2022

Windows Forms Application

In contrast to the Console Application, the Windows Forms application template is used when you need to create an app that has a UI. The project template (like the Console Application) can run on the .NET Framework or .NET 6, as seen in Figure 2-9.

Figure 2-9.  Windows Forms App (.NET 6 and .NET Framework)

It is important to note that before .NET 6, there were two separate products: the

.NET Framework and .NET Core. If you’re part of the Old Guard, you will have worked extensively with the .NET Framework. With .NET Core, however, a process was set in motion that would change the face of .NET development forever. Being open source,

.NET Core was rewritten from scratch and is designed to work across all platforms, such as Windows, Linux, and Mac.

Now, with .NET 6.0 going forward, developers will have just a single version of .NET (and it’s just called .NET). Think of .NET 6 as a unified development platform that allows developers to create desktop, cloud, web, mobile, gaming, IoT, and AI applications.

Windows Service

If you ever need to create a Windows application that continually runs in the background, performing some specific task, your best choice would be to use a Windows Service template as seen in Figure 2-10.

Figure 2-10.  Windows Service (.NET Framework)

71

Chapter 2 Working with Visual Studio 2022

Imagine that the application needs to monitor specific activity (be that in a database or file system) and then write messages to an event log. A Windows Service is ideally suited for this purpose.

Windows Services have an OnStart method that allows you to define what needs to happen when the service starts. By definition, Windows Services are long-running applications that need to poll or monitor the system it runs on. You will need to use a Timer component to enable the polling functionality.

A common mistake is to use a Windows Forms Timer for a Windows Service. You must ensure that you use the timer in the System.Timers.Timer namespace instead.

The System.Timers timer (Figure 2-11) that you add to the Windows Service will raise an Elapsed event at specific intervals.

Figure 2-11.  Various timer namespaces

In this Elapsed event, you can write the code your service needs to run to do what it needs to do.

72