Reputation: 172
I have a program to set the wallpaper of the user to a file. I need to get the full path to the image file
Here is my code
#include <windows.h>
int main()
{
FreeConsole();
const wchar_t *filenm = L"hay.jpg"; //Get path to hay.jpg
bool isWallSet = SystemParametersInfoW(SPI_SETDESKWALLPAPER, 0,
(void*)filenm, SPIF_UPDATEINIFILE);
return 0;
}
Upvotes: 3
Views: 22587
Reputation: 8866
Assuming the file in question is within your current working directory, GetFullPathName
sounds like a good idea. It takes a filename and converts it into a full path by prepending the current working directory.
Pay careful attention to the warning in the docs: you can't safely use this API in a multithreaded program, because the current working directory is saved as a process-wide global variable which isn't thread-safe to access.
Also be aware the API will return a path whether the file exists within the working directory or not; it just takes the filename you give it and prepends the current working directory.
#include <windows.h>
int main()
{
char filename[] = "test.txt";
char fullFilename[MAX_PATH];
GetFullPathName(filename, MAX_PATH, fullFilename, nullptr);
MessageBox(NULL, fullFilename, "DEBUG", MB_OK);
}
Upvotes: 7
Reputation: 909
If you use Viual Studio 2015, you can use the new filesystem library for this.
#include <filesystem>
using filesystem = std::experimental::filesystem::v1;
...
const wchar_t *filename = filesystem::canonical(L"hay.jpg").c_str();
...
Upvotes: 6