Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission denied when opening a file on desktop with fopen

I'm trying the following code, but it returns EACCESS (permission denied). I really don't know why. I even tried to run VS in Administrator mode, without success. It always works well.. maybe because it is windows universal app?

auto err = _wfopen_s(&f, L"C:/Users/Lukas/Desktop/Audio.mp3", L"rb");
if (err > 0)
    return;

fseek(f, SEEK_END, 0);
unsigned int size = ftell(f);
fseek(f, SEEK_SET, 0);

char *data = new char[size];
fread(data, 1, size, f);

fclose(f);
like image 280
Quest Avatar asked Dec 04 '25 02:12

Quest


1 Answers

This is expected for a Universal Windows app. Windows Runtime apps run in a sandbox and do not have access to arbitrary parts of the file system. They can directly access (e.g. via fopen, etc.)only their appx package (read only) and app data (read write) directories. All other locations are available only with user permission through the StorageFile and StorageFolder broker objects.

To access your MP3 on the desktop you'll need to have the user select it with a file picker (or equivalent) to get a StorageFile with appropriate permissions, which can then be cached for later use with the AccessCache classes. Alternatively, put the MP3 in the music library and the app can declare the music library capability and get a StorageFile via the KnownFolders.MusicLibrary folder.

I go into more detail in my blog entry at http://blogs.msdn.com/b/wsdevsol/archive/2012/12/05/stray-from-the-path-stick-to-the-storagefile.aspx

like image 151
Rob Caplan - MSFT Avatar answered Dec 05 '25 16:12

Rob Caplan - MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!