c#发展

注册

 

发新话题 回复该主题

c中用SystemDiagnostic [复制链接]

1#
北京哪家治疗白癜风好 https://wapjbk.39.net/yiyuanzaixian/bjzkbdfyy/bdf/

vlc.exe.lnk双击这个文件,能正常打开vlc,但是用System.Diagnostics.Process.Start(Path.GetFullPath("vlc.exe.lnk"),url);没有任何反应。根据常理,不应该出现这个问题。但是现实就是这么魔幻,偏偏有这个问题。

在这里插入图片描述

根据上面图,根据快捷方式是可以获取到vlc可执行文件的路径的,然后在网上搜索到这段代码,如下:

//引用→添加引用→左侧菜单的COM选项→勾上MicrosoftShellControlsAndAutomationusingShell32;namespaceFD.WinformUI{publicclassUICommon{//传入快捷方式文件的路径,返回目标exe文件的路径publicstringGetExePathFromShortcut(stringshortcutFilePath){FileInfofileInfo=newFileInfo(shortcutFilePath);stringtargetPath="";if(fileInfo.Extension.ToLower()==".lnk")//判断是否为快捷方式文件{Shellshell=newShell();Folderfolder=shell.NameSpace(fileInfo.DirectoryName);FolderItemfolderItem=folder.ParseName(fileInfo.Name);if(folderItem!=null){ShellLinkObjectlink=(ShellLinkObject)folderItem.GetLink;targetPath=link.Target.Path;//获取目标exe文件的路径}}elseif(fileInfo.Extension.ToLower()==".url")//判断是否为网址快捷方式{using(StreamReaderreader=newStreamReader(shortcutFilePath)){stringline;while((line=reader.ReadLine())!=null){if(line.StartsWith("URL=",StringComparison.OrdinalIgnoreCase)){targetPath=line.Substring(4).Trim();break;}}}}returntargetPath;}}}

调用上面的函数UICommon.Instance.GetExePathFromShortcut(Path.GetFullPath("vlc.exe.lnk")),得到的路径是"C:\ProgramFiles(x86)\VideoLAN\VLC\vlc.exe",而这个路径根本不存在,这应该是c#画蛇添足了,要去掉"(x86)"。用System.Diagnostics.Process.Start(

"C:\ProgramFiles(x86)\VideoLAN\VLC\vlc.exe",url);这样就能调用了,算是解决了这个问题。

后来我尝试用System.Diagnostics.Process.Start(UICommon.Instance.GetExePathFromShortcut(Path.GetFullPath("vlc.exe.lnk")),url);结果又傻眼了,发现调用了这个函数,vlc根本无法启动。后来打印日志,发现日志卡住了,这意思就是程序卡住了。后来干脆在项目的主函数里调用System.Diagnostics.Process.Start(UICommon.Instance.GetExePathFromShortcut(Path.GetFullPath("vlc.exe.lnk")),url);结果vlc能启动,并且能正常打开

分享 转发
TOP
发新话题 回复该主题