问题
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:
- 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? - 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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/51781921/programmatically-change-windows-10-lock-screen-background-on-desktop