为什么我在通过 Shell32 读取公共快捷方式时收到 E_ACCESSDENIED?
我正在尝试读取 C# 4 应用程序中所有桌面快捷方式的目标。 Windows 桌面上的快捷方式可以来自多个位置,具体取决于快捷方式是为所有用户还是仅为当前用户创建。在这种特定情况下,我尝试从公共桌面读取快捷方式,例如从 C:\Users\Public\Desktop\shortcut.lnk 读取。
代码如下(path是一个包含lnk文件路径的字符串):
var shell = new Shell32.ShellClass();
var folder = shell.NameSpace(Path.GetDirectoryName(path));
var folderItem = folder.ParseName(Path.GetFileName(path));
if (folderItem != null)
{
var link = (Shell32.ShellLinkObject)folderItem.GetLink;
最后一行抛出System.UnauthorizedAccessException,表明不允许读取快捷方式文件的内容。我已经尝试过用户私人桌面(c:\Users\username\Desktop)上的快捷方式文件,效果很好。
所以,我的问题是:
(1)当我可以作为用户清楚地读取内容时,为什么我的应用程序不允许/读取/代码中的快捷方式?
(2) 有办法解决这个问题吗?也许为应用程序使用特殊的清单文件?
顺便说一下,我的操作系统是 Windows 7,64 位。
一切顺利
-h-
I'm trying to read the targets of all desktop shortcuts in a C# 4 application. The shortcuts on a windows desktop can come from more that one location, depending on whether the shortcut is created for all users or just the current user. In this specific case I'm trying to read a shortcut from the public desktop, e.g. from C:\Users\Public\Desktop\shortcut.lnk.
The code is like this (path is a string contaning the path to the lnk file):
var shell = new Shell32.ShellClass();
var folder = shell.NameSpace(Path.GetDirectoryName(path));
var folderItem = folder.ParseName(Path.GetFileName(path));
if (folderItem != null)
{
var link = (Shell32.ShellLinkObject)folderItem.GetLink;
The last line throws an System.UnauthorizedAccessException, indicating that it's not allowed to read the shortcut file's contents. I have tried on shortcut files on the user's private desktop (c:\Users\username\Desktop) and that works fine.
So, my questions are:
(1) why is my application not allowed to /read/ the shortcut from code, when I can clearly read the contents as a user?
(2) is there a way to get around this? Maybe using a special manifest file for the application?
And, by the way, my OS is Windows 7, 64-bit.
be well
-h-
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,默认情况下您无法访问该文件夹中的 .lnk 文件。您正在创建一个允许您修改 .lnk 属性的 COM 对象。这需要管理员级别的帐户并关闭 UAC。
是的,您可以解决这个问题 带有清单。
Yes, you cannot get access to the .lnk file in that folder by default. You are creating a COM object that allows you to modify the .lnk properties. And that requires an administrator level account with UAC turned off.
Yes, you can fix that with a manifest.