|
■No91140 (魔界の仮面弁士) に追記
>>Windows10を使っています。そのスタートメニューに貼り付けてる複数のwebのリンクを保存する方法を教えてください
> そこそこ面倒かも知れません。
とりあえず *.url と *.website 形式のみ列挙するサンプル。(*.lnk は面倒なので省略)
言語指定が無かったので、とりあえず Visual Basic で記述しています。
Option Strict On
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Runtime.InteropServices.ComTypes
Imports System.Text
Module Module1
Sub Main()
Dim links As New List(Of String)()
Dim targetDirs As String() = {"%AppData%\Microsoft\Windows\Start Menu\", "%AllUsersProfile%\Microsoft\Windows\Start Menu\"}
For Each d In targetDirs.Select(AddressOf Environment.ExpandEnvironmentVariables)
Dim q As IEnumerable(Of String) = Enumerable.Empty(Of String)()
q = q.Concat(EnumerateAllFiles(d, "*.url"))
q = q.Concat(EnumerateAllFiles(d, "*.website"))
For Each lnk In q
Console.WriteLine("[" & Path.GetFileNameWithoutExtension(lnk) & "]")
Console.WriteLine(GetUrl(lnk))
Console.WriteLine()
Next
Next
End Sub
Iterator Function EnumerateAllFiles(path As String, searchPattern As String) As IEnumerable(Of String)
Try
For Each s In Directory.EnumerateFiles(path, searchPattern, SearchOption.TopDirectoryOnly)
Yield s
Next
Catch
End Try
Try
For Each d In Directory.EnumerateDirectories(path)
For Each s In EnumerateAllFiles(d, searchPattern)
Yield s
Next
Next
Catch
End Try
End Function
Private Function GetUrl(ByVal filePath As String) As String
Dim urlFile As Object = Nothing
Dim pFile As IPersistFile
Dim locator As IUniformResourceLocatorW
Try
urlFile = CreateObject("InternetShortcut")
pFile = DirectCast(urlFile, IPersistFile)
pFile.Load(filePath, 0)
locator = DirectCast(urlFile, IUniformResourceLocatorW)
Dim url As String = String.Empty
locator.GetUrl(url)
Return url
Finally
Marshal.ReleaseComObject(urlFile)
End Try
End Function
<ComImport(), Guid("CABB0DA0-DA57-11CF-9974-0020AFD79762")>
<InterfaceType(ComInterfaceType.InterfaceIsIUnknown)>
Private Interface IUniformResourceLocatorW
Sub SetUrl(<[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pcszURL As String, ByVal dwInFlags As Integer)
Sub GetUrl(<Out(), MarshalAs(UnmanagedType.LPWStr)> ByRef ppszURL As String)
Sub InvokeCommand(ByVal purlici As IntPtr)
End Interface
End Module
|