Friday, August 13, 2021

How To Publish Single File For .NET 5

Objective

To publish as single file for .NET 5 projects.


Problems

Typically, an .NET project is published with several files including .exe file, .dll files, .json files, .pdb file, etc.

Steps

1. In Solution Explorer, click the project item

2. The workspace will display .csproj file content

3. Add <IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract> into <PropertyGroup> element

4. On menu bar, select Build > Publish

5. The workspace now will display

6. Click "Show all settings", a modal dialog "Profile settings" pop out

7. Set "Deployment mode" as "Self-contained", expand the "File publish options" item, check "Produce single file"

8. Click "Save" button, "Publish" button, and wait for publish result success.

9. After that, go to target location directory to check, we will get 2 files: .exe and .pdb.

10. Notice that .exe file will be much larger than before one since all native libraries are included into one .exe file. The .pdb file can be ignored in order to run the .exe file.


Conclusion

Several files can be embedded into 1 .exe file by using steps above, but the .exe file will becomes much larger than the one which is published as normal. However, this method merely works for .NET 5 projects.

Monday, October 21, 2019

C# Winforms: How to Override High DPI Scaling at Runtime

INTRODUCTION

Sometimes, we set custom scaling to our desktop in order to have comfortable look. However, setting custom scaling could make the text of our winform application blur, as shown in figure below.
Figure 1


We can manually make the text clear by setting the application properties. First, right click the application .exe file, go to the "Compatibility" tab, then click the "Change high DPI settings" button, as shown in the figure below.
Figure 2


A dialog box pop out, we then check the option as shown in the figure below.
Figure 3


After that, we run the application again, the text is shown clear as shown in the figure below, compare to the Figure 1 above.
Figure 4


Objective of this article is to create a winform application which overrides high DPI by using C# as shown in the next section.


OVERRIDE HIGH DPI

The C# source code below illustrates MyForm class.

The C# source code below implements the Main method in the MainProgram class.

The main point here is the SetProcessDPIAware method. SetProcessDPIAware method is imported from the user32.dll file.

The SetProcessDPIAware method must be called before any GUI control is created, and it only supports from Windows Vista or later. Thus, we must check the Windows OS version before the SetProcessDPIAware method is called.


At last, we build and run it. The result is shown as same as Figure 4.

CONCLUSION

Conclusion: To override high DPI, we import the SetProcessDPIAware method from user32.dll file, and call it before any GUI control is created.

REFERENCE

For more information about SetProcessDPIAware function, see SetProcessDPIAware function (winuser.h) | Microsoft Docs.
For more information about EnableVisualStyles method, see Application.EnableVisualStyles | Microsoft Docs.
For more information about RenderWithVisualStyles method, see Application.RenderWithVisualStyles | Microsoft Docs.

How To Publish Single File For .NET 5

Objective To publish as single file for .NET 5 projects. Problems Typically, an .NET project is published with several files including .exe ...