使用fopen在桌面上打开文件时权限被拒绝(Permission denied when opening a file on desktop with fopen)

编程入门 行业动态 更新时间:2024-10-28 01:24:27
使用fopen在桌面上打开文件时权限被拒绝(Permission denied when opening a file on desktop with fopen)

我正在尝试以下代码,但它返回EACCESS(权限被拒绝)。 我真的不知道为什么。 我甚至尝试在管理员模式下运行VS,但没有成功。 它总是运作良好..也许是因为它是Windows通用应用程序?

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);

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);

最满意答案

这是通用Windows应用程序的预期。 Windows运行时应用程序在沙箱中运行,无法访问文件系统的任意部分。 他们可以直接访问(例如通过fopen等)他们的appx包(只读)和app数据(读写)目录。 只有通过StorageFile和StorageFolder代理对象的用户权限才能使用所有其他位置。

要在桌面上访问您的MP3,您需要让用户使用文件选择器(或等效文件)选择它,以获得具有适当权限的StorageFile,然后可以将其缓存以供以后与AccessCache类一起使用。 或者,将MP3放入音乐库,应用程序可以声明音乐库功能,并通过KnownFolders.MusicLibrary文件夹获取StorageFile。

我在博客文章中详细介绍了http://blogs.msdn.com/b/wsdevsol/archive/2012/12/05/stray-from-the-path-stick-to-the-storagefile.aspx

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

更多推荐

本文发布于:2023-04-28 15:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1332453.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:被拒   权限   文件   桌面上   fopen

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!