小编use*_*587的帖子

如何从文件快捷方式获取路径名?获得例外

可能重复:
获取快捷方式文件夹的目标

例如,在C:\TEMP\我有一个名为test.dll快捷方式的快捷方式将导致文件名test.dll

我想从快捷方式只获取路径名到它自己的文件.所以,我在另一个递归函数中调用此函数,并在每次从我的硬盘中的另一个目录中放入此函数.

例如,第一个目录C:\TEMP就在C:\TEMP那里有快捷方式文件,我只想获取文件的路径.在C:\TEMP测试中,我现在有3个文件:

hpwins23.dat
hpwmdl23.dat
hpwmdl23.dat - Shortcut(C:\TEMP\hpwmdl23.dat)

所以,我想得到的是快捷方式的路径名,本例中是C:\ TEMP

我试着使用这个功能:

public string GetShortcutTargetFile(string shortcutFilename)
        {
            string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
            string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);
            Shell shell = new Shell();
            Folder folder = shell.NameSpace(pathOnly);
            if (folder == null)
            {
            }
            else
            {
                FolderItem folderItem = folder.ParseName(filenameOnly);
                if (folderItem != null)
                {
                    Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
                    return link.Path;
                }
            }
            return string.Empty;
        }
Run Code Online (Sandbox Code Playgroud)

但是当我使用该函数并且它到达一个快捷方式时,我遇到异常错误:

Shell32.ShellLinkObject link …
Run Code Online (Sandbox Code Playgroud)

c# windows-shell

9
推荐指数
1
解决办法
2万
查看次数

如何从字符串中删除"http://"部分?

我有这个方法:

private List<string> offline(string targetDirectory)
{
    if (targetDirectory.Contains("http://"))
    {
        MessageBox.Show("true");
    }
    DirectoryInfo di = new DirectoryInfo(targetDirectory);
    List<string> directories = new List<string>();

    try
    {
        string[] dirs = Directory.GetDirectories(targetDirectory,"*.*",SearchOption.TopDirectoryOnly);
        for (int i = 0; i < dirs.Length; i++)
        {
            string t = "http://" + dirs[i];
            directories.Add(t);
        }
    }
    catch
    {
        MessageBox.Show("hgjghj");
    }


    return directories;

}
Run Code Online (Sandbox Code Playgroud)

这是部分:

if (targetDirectory.Contains("http://"))
{
     MessageBox.Show("true");
}
Run Code Online (Sandbox Code Playgroud)

我正在获取一个目录,它给我这个目录中的所有目录,并且我将每个目录添加到字符串中"http://".

问题是当下一次目录到达它所带来的功能时 "http://"

例如:http://c:\\http://c:\\windows

然后就行了

 DirectoryInfo di = new DirectoryInfo(targetDirectory); // throws exception.
Run Code Online (Sandbox Code Playgroud)

所以我希望每次目录到达函数时检查它是否从头"http://"开始,剥离 …

c#

8
推荐指数
2
解决办法
1万
查看次数

标签 统计

c# ×2

windows-shell ×1