vb.net 文件监视返回不正确的文件名

发布于 2024-08-19 22:11:38 字数 952 浏览 3 评论 0原文

我正在尝试使用 VB.Net FileSystemWatcher 类来监视文件夹以创建 Excel 文件。在文件夹中创建 .xls 文件时,我确实得到了响应,但以下代码有问题:

Private Sub AddWatch()

Dim watch As New FileSystemWatcher

        AddHandler watch.Changed, AddressOf FileChange
        watch.Filter = "*.*"
        watch.NotifyFilter = NotifyFilters.LastWrite
        watch.Path = "C:\Documents and Settings\my.user\Desktop\testing"
        watch.EnableRaisingEvents = True
End Sub

    Private Sub FileChange(ByVal obj As Object, ByVal e As System.IO.FileSystemEventArgs)

        Debug.Print("changetype is: " & e.ChangeType.ToString & ", path is: " & e.FullPath.ToString)
    End Sub

当我在此文件夹中创建文本文件时,我得到正确的文件名(“C:\Documents and Settings”) \my.user\Desktop\testing\foo.txt")。但是,当我将 Excel 文件保存到文件夹中时,路径仍然正确,但文件名是垃圾(即使文件名相同,每次也不同,总是 8 个字符,如“C:\Documents and Settings\my.user\Desktop”) \testing\DE0B5800"。)在 Google 或此处搜索中找不到任何内容,而 MSDN 像往常一样没什么帮助。有人以前遇到过这个或者知道我在哪里可以找到更多信息吗?

I am attempting to use the VB.Net FileSystemWatcher class to watch a folder for the creation of Excel files. I do get a response when a .xls file is created in the folder, but am having a problem with the below code:

Private Sub AddWatch()

Dim watch As New FileSystemWatcher

        AddHandler watch.Changed, AddressOf FileChange
        watch.Filter = "*.*"
        watch.NotifyFilter = NotifyFilters.LastWrite
        watch.Path = "C:\Documents and Settings\my.user\Desktop\testing"
        watch.EnableRaisingEvents = True
End Sub

    Private Sub FileChange(ByVal obj As Object, ByVal e As System.IO.FileSystemEventArgs)

        Debug.Print("changetype is: " & e.ChangeType.ToString & ", path is: " & e.FullPath.ToString)
    End Sub

When I create a text file in this folder, I get back the proper filename("C:\Documents and Settings\my.user\Desktop\testing\foo.txt"). However, when I save an Excel file into the folder, the path is still correct, but the filename is garbage (differs each time even with the same filename, always 8 characters like "C:\Documents and Settings\my.user\Desktop\testing\DE0B5800".) Can't find a thing on this searching Google or here, and MSDN as usual is little help. Anyone run across this before or know where I can find more information?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

定格我的天空 2024-08-26 22:11:38

http://support.microsoft.com/kb/814068

总之:

当 Excel 保存文件时,Excel 会跟随
这些步骤:

  1. Excel 创建一个随机命名的临时文件(例如,Cedd4100
    没有文件扩展名)
    您指定的目标文件夹
    在“另存为”对话框中。

    工作簿写入临时
    文件。
  2. 如果将更改保存到现有文件,Excel 会删除该文件
    原始文件。
  3. Excel 重命名临时文件。 Excel给出临时文件
    您指定的文件名(例如
    “另存为”对话框中的“Book1.xls”)
    框。

http://support.microsoft.com/kb/814068

In summary:

When Excel saves a file, Excel follow
these steps:

  1. Excel creates a randomly named temporary file (for example, Cedd4100
    with no file name extension) in the
    destination folder that you specified
    in the Save As dialog box.
    The whole
    workbook is written to the temporary
    file.
  2. If changes are being saved to an existing file, Excel deletes the
    original file.
  3. Excel renames the temporary file. Excel gives the temporary file
    the file name that you specified (such
    as Book1.xls) in the Save As dialog
    box.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文