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.
See Call Windows Runtime APIs in desktop apps, specifically Use the Target Framework Moniker option:
<TargetFramework>net7.0-windows10.0.19041.0</TargetFramework>
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.