从电脑上拖动任一文件到 此控件 获取其完整路径. 包括快捷方式 非文件夹
麻烦了
14 个解决方案
#1
Dim dlg As New OpenFileDialog()
dlg.Multiselect = True
If dlg.ShowDialog() = DialogResult.OK Then
Dim filesName As String() = dlg.FileNames
For Each info As String In filesName
Dim f As File = New FileInfo(info)
Next
End If
dlg.Multiselect = True
If dlg.ShowDialog() = DialogResult.OK Then
Dim filesName As String() = dlg.FileNames
For Each info As String In filesName
Dim f As File = New FileInfo(info)
Next
End If
#2
晕了。。。。我还以为有人给出正确答案啊。。。。1楼的前辈,楼主想要拖动获取文件名
应该是从dragdrop类搞出来的吧,你可以去MSDN查查这个类
应该是从dragdrop类搞出来的吧,你可以去MSDN查查这个类
#3
最簡單範例
準備工作
1. 建立一個 Form, 並且拉一個 listBox 元件到上面
2. 命名 listBox 為 listBox_FileList
執行步驟
// Step 1: 拖拉功能啟動
this.listBox_FileList.AllowDrop = true;
// Step 2: 在 listBox_FileList 的 DragEnter 事件中, 加入下面程式碼
private void listBox_FileList_DragEnter(object sender, DragEventArgs e)
{
// 確定使用者抓進來的是檔案
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
{
// 允許拖拉動作繼續 (這時滑鼠游標應該會顯示 +)
e.Effect = DragDropEffects.All;
}
}
// Step 3: 在 listBox_FileList 的 DragDrop 事件中, 加入下面程式碼
private void listBox_FileList_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
listBox_FileList.Items.Add(file);
}
}
完成 !!
#4
UP
#5
Public Class Form1
Dim Path As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GroupBox1.AllowDrop = True
End Sub
Private Sub GroupBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles GroupBox1.DragDrop
Dim Files As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
For Each File As String In Files
Path = File
Next
End Sub
Private Sub GroupBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles GroupBox1.DragEnter
' 確定使用者抓進來的是檔案
If e.Data.GetDataPresent(DataFormats.FileDrop, False) = True Then
' 允許拖拉動作繼續 (這時滑鼠游標應該會顯示 +)
e.Effect = DragDropEffects.All
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TextBox1.Text = Path
End Sub
End Class
\Desktop\123.lnk 这个快捷方式怎么获取其实际路径呢?
#6
Private Sub GroupBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles GroupBox1.DragEnter
' 確定使用者抓進來的是檔案
If e.Data.GetDataPresent(DataFormats.FileDrop, False) = True Then
' 允許拖拉動作繼續 (這時滑鼠游標應該會顯示 +)
e.Effect = DragDropEffects.All
End If
End Sub
这块替换为:
Private Sub GroupBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles GroupBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Link
Else
e.Effect = DragDropEffects.None
End If
End Sub
另外,很多文本框的操作,拖动等常见操作,都在我曾给老婆写的例子中有。
你可以下载:
高仿windows的记事本源码,比系统的更强,能智能识别文件编码,效果很好,已测。
#7
很不错哦
不过我提个问题哦,我看你这个里面是不是这样来说才能显示所有的路径哦,不然一直就是一个路径
Private Sub GroupBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles GroupBox1.DragDrop
Dim Files As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
For Each File As String In Files
Path = File
TextBox1.Text = Path & vbNewLine & TextBox1.Text
Next
End Sub
#8
打开文件,拖多个没什么必要。不支持多个拖放也正常。
#9
Public Class Form1
Dim Path As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GroupBox1.AllowDrop = True
End Sub
Private Sub GroupBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles GroupBox1.DragDrop
Dim Files As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
Dim Test As String()
For Each File As String In Files
Test = Split(File, ".")
If Test(1) = "lnk" Then
MsgBox("文件格式不支持快捷方式") '这个快捷方式咋获得其目标路径呢?
Else
Path = File
End If
TextBox1.Text = Path
Next
End Sub
Private Sub GroupBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles GroupBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop, False) = True Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.None
End If
End Sub
End Class
#10
你看我6楼的回复和附带的例子吧。说过了。不想重复了。例子里有,可以拖快捷方式进来的。
#11
那个..我不是打开文件.
单独想获取 快捷方式 的目标 路径
单独想获取 快捷方式 的目标 路径
#12
#13
Dim sc As ShortCutFClass
'--
sc = New ShortCutFClass
Me.Label1.Text = sc.GetLnkFileInfo(Me.TextBox1.Text)
Imports IWshRuntimeLibrary
Public Class ShortCutFClass
Public Sub New()
End Sub
''' <summary>
''' 获取快捷方式的目标文件的路径。即要运行的程序的路径。
''' </summary>
''' <param name="LnkFilePath">快捷方式文件的路径。</param>
''' <returns>目标文件的路径</returns>
''' <remarks></remarks>
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
''' <summary>
''' 创建一个快捷方式。成功返回:True,否则返回:False。
''' </summary>
''' <param name="lnkFile">快捷方式的存放路径。</param>
''' <param name="ExeFilePath">要运行的程序的目标文件</param>
''' <param name="iDescription">描述</param>
''' <returns>成功返回:True,否则返回:False</returns>
''' <remarks></remarks>
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
#14
http://blog.csdn.net/rijing2000/archive/2005/03/21/325589.aspx
http://www.google.com.hk/search?ie=GB2312&hl=zh-CN&q=IWshRuntimeLibrary
http://www.google.com.hk/search?ie=GB2312&hl=zh-CN&q=IWshRuntimeLibrary
#1
Dim dlg As New OpenFileDialog()
dlg.Multiselect = True
If dlg.ShowDialog() = DialogResult.OK Then
Dim filesName As String() = dlg.FileNames
For Each info As String In filesName
Dim f As File = New FileInfo(info)
Next
End If
dlg.Multiselect = True
If dlg.ShowDialog() = DialogResult.OK Then
Dim filesName As String() = dlg.FileNames
For Each info As String In filesName
Dim f As File = New FileInfo(info)
Next
End If
#2
晕了。。。。我还以为有人给出正确答案啊。。。。1楼的前辈,楼主想要拖动获取文件名
应该是从dragdrop类搞出来的吧,你可以去MSDN查查这个类
应该是从dragdrop类搞出来的吧,你可以去MSDN查查这个类
#3
最簡單範例
準備工作
1. 建立一個 Form, 並且拉一個 listBox 元件到上面
2. 命名 listBox 為 listBox_FileList
執行步驟
// Step 1: 拖拉功能啟動
this.listBox_FileList.AllowDrop = true;
// Step 2: 在 listBox_FileList 的 DragEnter 事件中, 加入下面程式碼
private void listBox_FileList_DragEnter(object sender, DragEventArgs e)
{
// 確定使用者抓進來的是檔案
if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
{
// 允許拖拉動作繼續 (這時滑鼠游標應該會顯示 +)
e.Effect = DragDropEffects.All;
}
}
// Step 3: 在 listBox_FileList 的 DragDrop 事件中, 加入下面程式碼
private void listBox_FileList_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
{
listBox_FileList.Items.Add(file);
}
}
完成 !!
#4
UP
#5
Public Class Form1
Dim Path As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GroupBox1.AllowDrop = True
End Sub
Private Sub GroupBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles GroupBox1.DragDrop
Dim Files As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
For Each File As String In Files
Path = File
Next
End Sub
Private Sub GroupBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles GroupBox1.DragEnter
' 確定使用者抓進來的是檔案
If e.Data.GetDataPresent(DataFormats.FileDrop, False) = True Then
' 允許拖拉動作繼續 (這時滑鼠游標應該會顯示 +)
e.Effect = DragDropEffects.All
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
TextBox1.Text = Path
End Sub
End Class
\Desktop\123.lnk 这个快捷方式怎么获取其实际路径呢?
#6
Private Sub GroupBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles GroupBox1.DragEnter
' 確定使用者抓進來的是檔案
If e.Data.GetDataPresent(DataFormats.FileDrop, False) = True Then
' 允許拖拉動作繼續 (這時滑鼠游標應該會顯示 +)
e.Effect = DragDropEffects.All
End If
End Sub
这块替换为:
Private Sub GroupBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles GroupBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Link
Else
e.Effect = DragDropEffects.None
End If
End Sub
另外,很多文本框的操作,拖动等常见操作,都在我曾给老婆写的例子中有。
你可以下载:
高仿windows的记事本源码,比系统的更强,能智能识别文件编码,效果很好,已测。
#7
很不错哦
不过我提个问题哦,我看你这个里面是不是这样来说才能显示所有的路径哦,不然一直就是一个路径
Private Sub GroupBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles GroupBox1.DragDrop
Dim Files As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
For Each File As String In Files
Path = File
TextBox1.Text = Path & vbNewLine & TextBox1.Text
Next
End Sub
#8
打开文件,拖多个没什么必要。不支持多个拖放也正常。
#9
Public Class Form1
Dim Path As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
GroupBox1.AllowDrop = True
End Sub
Private Sub GroupBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles GroupBox1.DragDrop
Dim Files As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
Dim Test As String()
For Each File As String In Files
Test = Split(File, ".")
If Test(1) = "lnk" Then
MsgBox("文件格式不支持快捷方式") '这个快捷方式咋获得其目标路径呢?
Else
Path = File
End If
TextBox1.Text = Path
Next
End Sub
Private Sub GroupBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles GroupBox1.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop, False) = True Then
e.Effect = DragDropEffects.All
Else
e.Effect = DragDropEffects.None
End If
End Sub
End Class
#10
你看我6楼的回复和附带的例子吧。说过了。不想重复了。例子里有,可以拖快捷方式进来的。
#11
那个..我不是打开文件.
单独想获取 快捷方式 的目标 路径
单独想获取 快捷方式 的目标 路径
#12
#13
Dim sc As ShortCutFClass
'--
sc = New ShortCutFClass
Me.Label1.Text = sc.GetLnkFileInfo(Me.TextBox1.Text)
Imports IWshRuntimeLibrary
Public Class ShortCutFClass
Public Sub New()
End Sub
''' <summary>
''' 获取快捷方式的目标文件的路径。即要运行的程序的路径。
''' </summary>
''' <param name="LnkFilePath">快捷方式文件的路径。</param>
''' <returns>目标文件的路径</returns>
''' <remarks></remarks>
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
''' <summary>
''' 创建一个快捷方式。成功返回:True,否则返回:False。
''' </summary>
''' <param name="lnkFile">快捷方式的存放路径。</param>
''' <param name="ExeFilePath">要运行的程序的目标文件</param>
''' <param name="iDescription">描述</param>
''' <returns>成功返回:True,否则返回:False</returns>
''' <remarks></remarks>
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
#14
http://blog.csdn.net/rijing2000/archive/2005/03/21/325589.aspx
http://www.google.com.hk/search?ie=GB2312&hl=zh-CN&q=IWshRuntimeLibrary
http://www.google.com.hk/search?ie=GB2312&hl=zh-CN&q=IWshRuntimeLibrary