Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 未能找到快捷方式的目标_C#_Console Application_Shortcut - Fatal编程技术网

C# 未能找到快捷方式的目标

C# 未能找到快捷方式的目标,c#,console-application,shortcut,C#,Console Application,Shortcut,我试图创建一个小程序,向用户检索C#(控制台应用程序)中快捷方式的目标文件名。 我的代码可以正常工作,但没有给出正确的结果 这是我的代码:(摘自:) 正如我所说,检索我的代码输出错误(空字符串),即使文件存在。 例如,我很想在路径C:\Users\Admin123\AppData\Roaming\Microsoft\Office\Recent中获取某个快捷方式的目标文件 这个问题的原因是什么?我该怎么解决呢 编辑 我再次尝试了相同的代码,现在它工作了!谢谢大家!:) 我看你的代码没有问题。经过测

我试图创建一个小程序,向用户检索C#(控制台应用程序)中快捷方式的目标文件名。 我的代码可以正常工作,但没有给出正确的结果

这是我的代码:(摘自:)

正如我所说,检索我的代码输出错误(空字符串),即使文件存在。 例如,我很想在路径C:\Users\Admin123\AppData\Roaming\Microsoft\Office\Recent中获取某个快捷方式的目标文件

这个问题的原因是什么?我该怎么解决呢

编辑
我再次尝试了相同的代码,现在它工作了!谢谢大家!:)

我看你的代码没有问题。经过测试,效果良好

我创建了两个链接:
boot.lnk
prestigio_notes.lnk
,这两个链接都指向正确的文件。他们的产出是:

D:\Boot1.asm
D:\Dokumenty\Android\Prestigio\doc\Prestigio\u notes.txt分别

这是我使用的代码(很抱歉再次复制粘贴函数,但我希望它是一个完整且清晰的类):


检查是否从“可用引用”列表中引用了正确的COM对象。如果失败,请检查您是否有权从指定位置读取。

我认为您需要使用COM互操作服务才能成功使用Shell32.dll组件。我从来没有真正的理由使用它,所以我不想给你指出太远的错误方向。下面是关于COM互操作的MSDN文章:你只是想知道一个文件是否存在吗?你是对的,现在我的代码神奇地工作了。。无论如何谢谢你!
private static string GetTargetPath(string ShortcutPath)
{
    string pathOnly = System.IO.Path.GetDirectoryName(ShortcutPath);
    string filenameOnly = System.IO.Path.GetFileName(ShortcutPath);

    Shell32.Shell shell = new Shell32.ShellClass();
    Shell32.Folder folder = shell.NameSpace(pathOnly);
    Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
    if (folderItem != null)
    {
        Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
        return link.Path;
    }
    return ""; // not found
}
class Program {
    static void Main(string[] args) {
        Console.WriteLine(GetTargetPath(@"D:\boot.lnk"));
        Console.WriteLine(GetTargetPath(@"D:\prestigio_notes.lnk"));
        Console.ReadLine();
    }

    private static string GetTargetPath(string ShortcutPath) {
        string pathOnly = System.IO.Path.GetDirectoryName(ShortcutPath);
        string filenameOnly = System.IO.Path.GetFileName(ShortcutPath);

        Shell32.Shell shell = new Shell32.Shell();
        Shell32.Folder folder = shell.NameSpace(pathOnly);
        Shell32.FolderItem folderItem = folder.ParseName(filenameOnly);
        if (folderItem != null) {
            Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
            return link.Path;
        }
        return ""; // not found
    }
}