求教一个获取路径的问题?

DMwuzei 2010-04-24 09:09:08
我做了个filewacher,来监视C:\test.txt文本文件,代码是这样的.(直接把路径写了进去,这样可以实现效果)

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Me.filewatcher.Path = "C:\" --------------------------------------(如何修改?)
Me.filewatcher.NotifyFilter = NotifyFilters.Size
Me.filewatcher.Filter = "test.txt" -------------------------------(如何修改?)
AddHandler Me.filewatcher.Changed, New FileSystemEventHandler(AddressOf Me.filewatcher_Changed)
Me.filewatcher.EnableRaisingEvents = True
End Sub


现在我想做个openfiledialog来实现获取 文本路径.

应该怎么写?
...全文
50 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
只在此山中 2010-04-24
  • 打赏
  • 举报
回复
Dim opfDlg As New OpenFileDialog
opfDlg.Filter = "*.*|*.*"
opfDlg.Multiselect = False
opfDlg.Title = "选择文件"
opfDlg.ShowDialog()
If opfDlg.FileName <> "" Then
Me.filewatcher.Path =opfDlg.FileName.Substring(0,opfDlg.FileName.LastIndexOf("\") + 1)
Me.filewatcher.Filter = opfDlg.FileName.Substring(opfDlg.FileName.LastIndexOf("\") + 1)

end if
兔子-顾问 2010-04-24
  • 打赏
  • 举报
回复
Imports System.IO

Public Class Form1
Dim filewatcher As New filesystemwatcher
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dlg As New OpenFileDialog()
dlg.Filter = "*.txt|*.txt"
If dlg.ShowDialog() <> Windows.Forms.DialogResult.OK Then Exit Sub
Me.filewatcher.NotifyFilter = NotifyFilters.Size
Me.filewatcher.Path = Path.GetPathRoot(dlg.FileName)
Me.filewatcher.Filter = Path.GetFileName(dlg.FileName)
AddHandler Me.filewatcher.Changed, AddressOf MyFileSystemEventHandler
Me.filewatcher.EnableRaisingEvents = True
End Sub
Public Sub MyFileSystemEventHandler(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
MessageBox.Show(e.FullPath)
End Sub
End Class

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧