Martin
Martin

Reputation: 73

Registry value may equal to null

How can I fix this issue where the VS tells me that it might output null value of Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop").GetValue("TranscodedImageCache")?

byte[] path = (byte[])Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop").GetValue("TranscodedImageCache");
String wallpaperFile = Encoding.Unicode.GetString(SliceMe(path, 24)).TrimEnd("\0".ToCharArray());

Upvotes: 0

Views: 261

Answers (1)

It all makes cents
It all makes cents

Reputation: 4983

If using .NET 6, try the following:

using RegistryKey? subkey = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop");
byte[]? path = (byte[]?)subkey?.GetValue("TranscodedImageCache");

                    ...

VS 2022:

In a .NET 6 project, the Nullable setting can be found:

  • In VS menu, click Project
  • Select <project name> Properties
  • Expand Build
  • Setting name is: Nullable

The following can also be seen in the .csproj file: <Nullable>enable</Nullable>

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

</Project>

Resources:

Here's some additional resources that may be of interest:

Upvotes: 1

Related Questions