VB.NET创建快捷方式和读取快捷方式的类
2007-11-22 18:59
429 查看
Imports IWshRuntimeLibrary '引用:Windows Script Host Object Model
Public Class LnkFileClass
Public Sub New()
End Sub
Public Function GetLnkFileInfo(ByVal LnkFilePath As String) As String
Dim iPos As Integer
iPos = LnkFilePath.LastIndexOf(".")
Dim tmp As String
tmp = LnkFilePath.Substring(iPos + 1)
If tmp.ToLower <> "lnk" Then
Return ""
End If
Try
Dim f As New IWshShell_Class
Dim Lnk As IWshShortcut
Lnk = CType(f.CreateShortcut(LnkFilePath), IWshShortcut)
f = Nothing
Return Lnk.TargetPath
Catch ex As Exception
Return ""
End Try
End Function
Public Function CreatLnkFile(ByVal lnkFile As String, ByVal ExeFilePath As String, ByVal iDescription As String) As Boolean
Try
If Not IO.Directory.Exists(ExeFilePath) Then
Dim retVal As DialogResult = MsgBox(ExeFilePath & " 目标文件不存在,你还要创造它吗?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo)
If retVal = Windows.Forms.DialogResult.Yes Then
IO.Directory.CreateDirectory(ExeFilePath)
Else
Return False
End If
End If
Dim iconNumber As Integer = 0
Dim CreatDir As String = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim wShell As New IWshShell_Class
Dim shortCut As IWshRuntimeLibrary.IWshShortcut
shortCut = CType(wShell.CreateShortcut(CreatDir & "/" & lnkFile & ".lnk"), IWshShortcut)
shortCut.TargetPath = ExeFilePath
shortCut.WindowStyle = 1
shortCut.Description = iDescription
shortCut.WorkingDirectory = ""
shortCut.IconLocation = ExeFilePath & ", " & iconNumber
shortCut.Save()
wShell = Nothing
Return True
Catch ex As System.Exception
Return False
End Try
End Function
End Class
通过字节搜索获得路径也不慢么:
Public Class LnkFileExePath
Private m_Stream As FileStream
Private m_Reader As BinaryReader
Public Sub New()
End Sub
Public Function GetLnkFileInfo(ByVal LnkFile As String) As String
GetLnkFileInfo = ""
Dim tmp As String = ""
Dim i As Integer = 0
Dim iFilePath As String = ""
Dim iPos As Integer = 0
Dim n As Integer = 0
Try
m_Stream = New FileStream(LnkFile, FileMode.Open, FileAccess.Read)
m_Reader = New BinaryReader(m_Stream)
Catch ex As Exception
Return ""
End Try
Try
Dim k As Integer = m_Reader.BaseStream.Length
For i = 260 To k
m_Reader.BaseStream.Seek(i, SeekOrigin.Begin)
iFilePath = Nextchars(1024, m_Reader)
If iFilePath.Substring(1, 2) = ":/" Then
iFilePath = iFilePath.Substring(0, InStr(iFilePath, Chr(0)) - 1)
If iFilePath.Length > 5 Then
If iFilePath.Substring(iFilePath.Length - 4) = ".exe" Then
m_Reader.Close()
m_Stream.Close()
Debug.WriteLine(i & " " & iFilePath)
Return iFilePath
End If
End If
End If
Next
Catch ex As Exception
m_Reader.Close()
m_Stream.Close()
Return ""
End Try
m_Reader.Close()
m_Stream.Close()
Return ""
End Function
Private Function Nextchars(ByVal Num As Integer, ByVal reader As BinaryReader) As String
Dim ch() As Byte
ReDim ch(Num - 1)
reader.Read(ch, 0, ch.Length)
Return Encoding.Default.GetString(ch, 0, ch.Length)
End Function
End Class
Public Class LnkFileClass
Public Sub New()
End Sub
Public Function GetLnkFileInfo(ByVal LnkFilePath As String) As String
Dim iPos As Integer
iPos = LnkFilePath.LastIndexOf(".")
Dim tmp As String
tmp = LnkFilePath.Substring(iPos + 1)
If tmp.ToLower <> "lnk" Then
Return ""
End If
Try
Dim f As New IWshShell_Class
Dim Lnk As IWshShortcut
Lnk = CType(f.CreateShortcut(LnkFilePath), IWshShortcut)
f = Nothing
Return Lnk.TargetPath
Catch ex As Exception
Return ""
End Try
End Function
Public Function CreatLnkFile(ByVal lnkFile As String, ByVal ExeFilePath As String, ByVal iDescription As String) As Boolean
Try
If Not IO.Directory.Exists(ExeFilePath) Then
Dim retVal As DialogResult = MsgBox(ExeFilePath & " 目标文件不存在,你还要创造它吗?", MsgBoxStyle.Question Or MsgBoxStyle.YesNo)
If retVal = Windows.Forms.DialogResult.Yes Then
IO.Directory.CreateDirectory(ExeFilePath)
Else
Return False
End If
End If
Dim iconNumber As Integer = 0
Dim CreatDir As String = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim wShell As New IWshShell_Class
Dim shortCut As IWshRuntimeLibrary.IWshShortcut
shortCut = CType(wShell.CreateShortcut(CreatDir & "/" & lnkFile & ".lnk"), IWshShortcut)
shortCut.TargetPath = ExeFilePath
shortCut.WindowStyle = 1
shortCut.Description = iDescription
shortCut.WorkingDirectory = ""
shortCut.IconLocation = ExeFilePath & ", " & iconNumber
shortCut.Save()
wShell = Nothing
Return True
Catch ex As System.Exception
Return False
End Try
End Function
End Class
通过字节搜索获得路径也不慢么:
Public Class LnkFileExePath
Private m_Stream As FileStream
Private m_Reader As BinaryReader
Public Sub New()
End Sub
Public Function GetLnkFileInfo(ByVal LnkFile As String) As String
GetLnkFileInfo = ""
Dim tmp As String = ""
Dim i As Integer = 0
Dim iFilePath As String = ""
Dim iPos As Integer = 0
Dim n As Integer = 0
Try
m_Stream = New FileStream(LnkFile, FileMode.Open, FileAccess.Read)
m_Reader = New BinaryReader(m_Stream)
Catch ex As Exception
Return ""
End Try
Try
Dim k As Integer = m_Reader.BaseStream.Length
For i = 260 To k
m_Reader.BaseStream.Seek(i, SeekOrigin.Begin)
iFilePath = Nextchars(1024, m_Reader)
If iFilePath.Substring(1, 2) = ":/" Then
iFilePath = iFilePath.Substring(0, InStr(iFilePath, Chr(0)) - 1)
If iFilePath.Length > 5 Then
If iFilePath.Substring(iFilePath.Length - 4) = ".exe" Then
m_Reader.Close()
m_Stream.Close()
Debug.WriteLine(i & " " & iFilePath)
Return iFilePath
End If
End If
End If
Next
Catch ex As Exception
m_Reader.Close()
m_Stream.Close()
Return ""
End Try
m_Reader.Close()
m_Stream.Close()
Return ""
End Function
Private Function Nextchars(ByVal Num As Integer, ByVal reader As BinaryReader) As String
Dim ch() As Byte
ReDim ch(Num - 1)
reader.Read(ch, 0, ch.Length)
Return Encoding.Default.GetString(ch, 0, ch.Length)
End Function
End Class
相关文章推荐
- VB.NET创建快捷方式和读取快捷方式的类
- VB.Net : ApplicationEvents类中,创建主程序的快捷方式 , 捕获程序未Try的错误及抛出相关的类和方法名 , 是否联机等相关。。
- VB.NET创建桌面快捷方式
- Asp.Net-创建网站的快捷方式到桌面,开始菜单,收藏夹
- .NET CF WM上创建快捷方式
- 用VB创建Windows快捷方式(无需DLL)
- 【转】Windows Mobile 快捷方式的读取和创建
- vb和dos批处理创建或生成快捷方式
- vb 创建快捷方式 兼容vista XP
- C#.net 创建网页快捷方式
- ASP.NET创建快捷方式
- 用VB创建开始菜单快捷方式(无需其他DLL)
- 【转载】C#.Net 创建网页快捷方式
- 用VB创建快捷方式(无需第三方DLL)
- vb.net 创建、修改、读取XML的方法
- 运用IShellLink和IPersistFile创建快捷方式,设置或读取快捷方式信息
- .NET CF WM上创建快捷方式
- Setup Factory 7.0获取安装包名称、读取.ini文件名、注册ActiveX组件、创建快捷方式、HTTP请求
- ASP.NET创建快捷方式
- windows shell 创建快捷方式和读取快捷方式的代码