none
Cannot Resolve Shortcut when RdOnly Attribute Set RRS feed

  • Question

  • I have tried hard but I do always fail when I attempt to resolve a shortcut where the shortcut itself has the RdOnly Attribute.

    I am using VS2017 and .NET Framework 4.6.2 with the following Code

    public static string ResolveShortcut(string path)
    {
    	string directory = Path.GetDirectoryName(path);
    	string file = Path.GetFileName(path);
    
    	Shell32.Shell shell = new Shell32.Shell();
    	Shell32.Folder folder = shell.NameSpace(directory);
    	Shell32.FolderItem folderItem = folder.ParseName(file);
    
    	Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink; // fails here!!!
    	return link.Path;
    }
    

    "GetLink" Fails with this error message:

    An unhandled exception of type 'System.UnauthorizedAccessException' occurred in PhotoCollection.exe

    Additional information: Zugriff verweigert (Ausnahme von HRESULT: 0x80070005 (E_ACCESSDENIED))

    So far, my only way out is to remove a readonly flag from the .LNK file, resolve the shortcut and then recover the prevoius setting of the readonly flag -- does not appear to be an elegant solution.

    Does anybody have a better idea?

    ph.

    Thursday, November 16, 2017 11:13 AM

Answers

  • It works for me with WshShell :

    I created a shortcut to Notepad and set it to Read Only, then

     // using IWshRuntimeLibrary;
    WshShell wshShell = new WshShell();
    string sShortcut = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Notepad.exe.lnk";
    if (System.IO.File.Exists(sShortcut))
    {
        IWshShortcut shortcut = (IWshShortcut)wshShell.CreateShortcut(sShortcut);
        string sTargetPath = shortcut.TargetPath;  
    }

    • Marked as answer by PHBecker Thursday, November 16, 2017 4:19 PM
    Thursday, November 16, 2017 11:57 AM

All replies

  • It works for me with WshShell :

    I created a shortcut to Notepad and set it to Read Only, then

     // using IWshRuntimeLibrary;
    WshShell wshShell = new WshShell();
    string sShortcut = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\Notepad.exe.lnk";
    if (System.IO.File.Exists(sShortcut))
    {
        IWshShortcut shortcut = (IWshShortcut)wshShell.CreateShortcut(sShortcut);
        string sTargetPath = shortcut.TargetPath;  
    }

    • Marked as answer by PHBecker Thursday, November 16, 2017 4:19 PM
    Thursday, November 16, 2017 11:57 AM
  • Short, beautiful -- and it works fine. Thank you for your advice.

    My urgent need for a solution has been satisfied. However, I am still wondering why I failed so badly with my original Code. Do I have to consider a bug in method "(Shell32.ShellLinkObject)folderItem.GetLink" or does anybody see a reason why Shell32 obviously attempts to write to the link?

    Thursday, November 16, 2017 4:26 PM
  • I traced the 2 methods (Windows 10, VS 2015) and there is :

    1) - NtCreateFile() with  FILE_OPEN and FILE_READ_ATTRIBUTES | GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE flags
    2) - NtCreateFile() with  FILE_OPEN and FILE_READ_ATTRIBUTES | GENERIC_READ | SYNCHRONIZE flags

    So the problem is GENERIC_WRITE in the first method, but I don't know why this flag is set to open the file...

    Thursday, November 16, 2017 6:24 PM