Results 1 to 6 of 6

Thread: Unusual file monitor results

  1. #1

    Thread Starter
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Unusual file monitor results

    Im trying to return the path of a saved word document. So im monitoring the directory for saved changes.

    If i save a document called sheet.docx i get these results. How come its not displaying just the full path and dcoument? C:\Documents and Settings\Owner\Desktop\New Folder\Sheet.docx as this is the entry i need



    here is my code

    vb Code:
    1. Private Sub fswDocumentDirectoryMonitor_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles fswDocumentDirectoryMonitor.Changed
    2.         Debug_LogFileChanged(e.FullPath)
    3.     End Sub
    4.  
    5.     ''' <summary>
    6.     ''' Logs that a file has changed in the debug log list view.
    7.     ''' </summary>
    8.     ''' <param name="pathToFile">The path to the file that was changed.</param>
    9.     ''' <remarks>
    10.     ''' Assumes pathToFile is not null.
    11.     ''' </remarks>
    12.     Private Sub Debug_LogFileChanged(ByVal pathToFile As String)
    13.         Dim count As Integer = lbMonitorDebug.Items.Count + 1
    14.         Dim message As String = String.Format("{0}- {1}  Changed  {2}", count, DateTime.Now, pathToFile)
    15.         lbMonitorDebug.Items.Add(message)
    16.     End Sub
    17.  
    18.     Private Sub fswDocumentDirectoryMonitor_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
    19.         Debug_LogFileCreated(e.FullPath)
    20.         ReturnCreatedFileAfterPrompting(e.FullPath)
    21.     End Sub
    22.  
    23.     ''' <summary>
    24.     ''' Logs that a file was created in the debug list box.
    25.     ''' </summary>
    26.     ''' <param name="fullPath">The full path to the file.</param>
    27.     Private Sub Debug_LogFileCreated(ByVal fullPath As String)
    28.         Dim count As Integer = lbMonitorDebug.Items.Count + 1
    29.         Dim message As String = String.Format("{0}- {1}  Created  {2}", count, DateTime.Now, fullPath)
    30.         lbMonitorDebug.Items.Add(message)
    31.     End Sub
    32.  
    33.     Private Sub ReturnCreatedFileAfterPrompting(ByVal filePath As String)
    34.  
    35.         Dim createdFile As New System.IO.FileInfo(filePath)
    36.  
    37.         Select Case createdFile.Extension.ToLower()
    38.             ' TODO: [AW] Magic list
    39.             Case ".doc", ".docx",
    40.                 MessageBox.Show(createdFile.ToString)
    41.         End Select
    42.  
    43.     End Sub
    44.  
    45.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    46.         Try
    47.             fswDocumentDirectoryMonitor.IncludeSubdirectories = True
    48.         Catch ex As Exception
    49.             ' TODO: [AW] Swallows all exceptions
    50.             MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    51.         End Try
    52.     End Sub
    53.  
    54.     ''' <summary>
    55.     ''' Toggles the state of the file system watcher.
    56.     ''' </summary>
    57.     Private Sub StartMonitorState()
    58.         Try
    59.  
    60.             If fswDocumentDirectoryMonitor.Path = "" Then
    61.  
    62.                 Dim message As String = "Please choose a directory to monitor"
    63.                 MessageBox.Show(message)
    64.                 Exit Sub
    65.             End If
    66.  
    67.             fswDocumentDirectoryMonitor.EnableRaisingEvents = True
    68.  
    69.         Catch ex As Exception
    70.             ' TODO: Swallows all exceptions
    71.             ' TODO: What errors are expected?
    72.             MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    73.         End Try
    74.     End Sub

  2. #2

    Thread Starter
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Unusual file monitor results

    any one?

  3. #3
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Unusual file monitor results

    Word does not just do a straight forward creation of a single file when you save a document. When you open a Word document it actually creates a temp file and write any changes you make to that, until you click Save and thats when it updates the original file. I presume when you create a new document it does a similar thing and creates some temp files then just renames one of them to the actual name of the document. If you are only monitoring for Created events then try monitoring LastModified as well and see what you get
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  4. #4

    Thread Starter
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Unusual file monitor results

    Quote Originally Posted by chris128 View Post
    Word does not just do a straight forward creation of a single file when you save a document. When you open a Word document it actually creates a temp file and write any changes you make to that, until you click Save and thats when it updates the original file. I presume when you create a new document it does a similar thing and creates some temp files then just renames one of them to the actual name of the document. If you are only monitoring for Created events then try monitoring LastModified as well and see what you get

    Ok, so what do you think the best way is to detect 2 newly created files?

    I use vb to create 2 word files and then save them manually.

    I was going to use a file monitor to get these 2 newly saved files path. Would make it quicker then manual;ly browsing for these files to email

    any suggestions?

  5. #5
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: Unusual file monitor results

    Why not just use VB to save them as well and then you already know where they are saved to?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  6. #6

    Thread Starter
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,401

    Re: Unusual file monitor results

    Quote Originally Posted by chris128 View Post
    Why not just use VB to save them as well and then you already know where they are saved to?
    But how could vb know what i called the file and and the directory since im saving the file out of vb ??? Since its a word document ..... even though i built it in vb?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width