0

I want to program a cute simple console app to set a random image from my image-folder as lock screen image. I just want to double click the program and, it searches the image folder, selects a random one and sets it as lock screen image without any user intervention.

My guess would be the following program:

Console.WriteLine("Set Random Lock Screen Image");

var myPicturesFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
var imageFileList = Directory.GetFiles(myPicturesFolder);
if (imageFileList.Length > 0)
{
  var rnd = new Random();
  var iNr = rnd.Next(imageFileList.Length);
  var newImage = imageFileList[iNr];

  Console.WriteLine($"set {newImage}");
  var storageFile = StorageFile.GetFileFromPathAsync(newImage).GetResults();
  LockScreen.SetImageFileAsync(storageFile).GetResults();
}

My only problem is, I can't find the assemblies containing StorageFile.GetFileFromPathAsync and LockScreen.SetImageFileAsync - what do I need to include to use these?

Or how else could I set the lock screen image? It does not need to be a console app, UWP would be fine, too, but I want to set the image without user interaction.

Sam
  • 28,421
  • 49
  • 167
  • 247
  • 1
    Where did you find the aforementioned functions? If you are copying them from a source you should be able to find the correct ```using``` directives to add. – UnsanitizedInput Apr 26 '23 at 12:11
  • From the MS sample https://learn.microsoft.com/de-de/samples/microsoft/windows-universal-samples/personalization/ But I could not deduct the assemblies from that project. Different platform use. – Sam Apr 26 '23 at 12:15
  • See [this post](https://stackoverflow.com/questions/57697292/how-to-change-the-lockscreen-back-ground-find-windows-system-userprofile-dll). Try using NuGet to install Uno.UI for StorageFile.... – UnsanitizedInput Apr 26 '23 at 12:22
  • Are you using _UWP_ as described in [Lock screen personalization sample](https://learn.microsoft.com/de-de/samples/microsoft/windows-universal-samples/personalization/): _Note: This sample is part of a large collection of UWP feature samples_ If not using _UWP_, search for [lock screen image windows 10 registry](https://www.google.com/search?q=lock+screen+image++windows+10+registry) – Tu deschizi eu inchid Apr 26 '23 at 16:26
  • Pity those samples do not work as console or other standalone app without user interaction :/ – Sam Apr 26 '23 at 20:41

1 Answers1

2

See Call Windows Runtime APIs in desktop apps, specifically Use the Target Framework Moniker option:

  • Change the target framework to gain access to Windows APIs:
<TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>
  • Now you have access to those namespaces and APIs:
using Windows.Storage;
using Windows.System.UserProfile;

try
{
    var myPicturesFolder = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
    var imageFileList = Directory.GetFiles(myPicturesFolder);
    var newImage = imageFileList[0];

    var storageFile = await StorageFile.GetFileFromPathAsync(newImage);
    await LockScreen.SetImageFileAsync(storageFile);
}
catch (Exception ex)
{
    Console.WriteLine("Error: " + ex);
}

I can execute the code though I receive the Access denied error understandably. You will need to get the valid permission either through Picker or manifest permission.

Luke Vo
  • 17,859
  • 21
  • 105
  • 181