7
votes

How can I programmatically change/set the lock screen background image (using VB.NET, C# or Visual C++)? I am using Win 10 Pro and have Visual Studio 2017 Pro.

I've had a look online but most solutions don't seem to work. For example:

  1. Both Change Win 10 Lockscreen Image Mutiple Times C# WPF and Change Windows Lock Screen background image in C# provide sample code but the imports System.Windows.Media.Imaging, Windows.Storage, Windows.Storage.Streams, Windows.System.UserProfile cannot be found when I paste the code. Do I need to add specific references?
  2. Lock screen personalization sample looks very promising but upon opening the solution/project file, Visual Studio throws an error saying that the provided format is unreadable.

Most sources (also Windows Dev Center - LockScreen Class) seem to suggest
Windows.System.UserProfile.LockScreen.SetImageFileAsync(file);
but I am apparently missing the specific namespace/import/package to make it run.

Any help would be appreciated.

2
in visual studio you can resolve the namespace by rightclicking the line and selecting the resolve. if the code needs a namespace. - Albert Laure
I've tried this, but it doesn't work like that. i.stack.imgur.com/uVmaa.png - ThomasT
Looks like you are using an ConsoleApplication but your links are for UWP and WPF - David Sdot
Which would be the right one to use? I see many WPF, but no UWP. i.stack.imgur.com/yxCz4.png - ThomasT
Have you got the Windows 10 SDK installed? Also, there's a Win10 version of the Lock screen personalisation sample here - dkwarr87

2 Answers

9
votes

I've found an easier way using this question Windows 10 Pro lockscreen mass change. All I need to do is set the registry entries under

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\PersonalizationCSP

  • DesktopImagePath
  • DesktopImageUrl
  • LockScreenImagePath
  • LockScreenImageUrl

to the path of the desired picture, which works without UWP or WPF.

1
votes

As alluded to in my comment, the Windows.System.UserProfile namespace is part of the UWP platform and requires having the Windows 10 SDK installed to utilize.

You can install the SDK by running the Visual Studio Installer, click "Modify" and then choose the "Universal Windows Platform development" workload, or choose the SDK from the list of "Individual Components". More details can be found here.

You may be able to use the library from a desktop .NET app as mentioned in this question How to Interop with Windows Runtime in .NET 4.5 and this link from the answer, but there's no guarantee that Microsoft will continue to allow that so you're probably better off developing using the UWP.

There is a sample from Microsoft for Lock Screen Personalisation on Windows 10 on GitHub here.