Results 1 to 20 of 20

Thread: filewather

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    25

    filewather

    hey im working on a program that reads the hash of a new file there is created!
    then if it match the database its say hash detected!

    but first time i create a new file it works fine, but when i try to delete the file
    its say the file is running in another program?

    source:
    Code:
    Private Sub fsw1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles fsw1.Created
    
    
            Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
            Dim f As FileStream = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
            f = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
            md5.ComputeHash(f)
            Dim hash As Byte() = md5.Hash
            Dim buff As StringBuilder = New StringBuilder
            Dim hashByte As Byte
            For Each hashByte In hash
                buff.Append(String.Format("{0:X2}", hashByte))
            Next
            f.Close()
            Dim newtext As New TextBox
            Dim read As String = My.Computer.FileSystem.ReadAllText("database.db")
            newtext.Text = read
            If newtext.ToString.Contains(buff.ToString) Then
                MsgBox("hash detected")
            Else
                MsgBox("hash not detected!")
            End If
    
    
    
    
    
    
        End Sub

  2. #2
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: filewather

    Note that in your code, you are actually instantiating two FileStream objects:
    Code:
    Dim f As FileStream = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
    f = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
    But, in the end, your f.Close() only closes the second one - you lose the reference to the first one when you reassign f on the second line of the code I copied.

    The solution of your problem would be to delete one of the constructor calls so you only create one FileStream object. You can also condense the code to this:
    Code:
    Dim f As New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    25

    Re: filewather

    Quote Originally Posted by obi1kenobi View Post
    Note that in your code, you are actually instantiating two FileStream objects:
    Code:
    Dim f As FileStream = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
    f = New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
    But, in the end, your f.Close() only closes the second one - you lose the reference to the first one when you reassign f on the second line of the code I copied.

    The solution of your problem would be to delete one of the constructor calls so you only create one FileStream object. You can also condense the code to this:
    Code:
    Dim f As New FileStream(e.FullPath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
    now it dont say the file is running in another process but when i create a another file its gives this error: exception has been thrown by the target of an invocation.

  4. #4
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: filewather

    Hmmm... That's the nastiest type of exception in my opinion. Try reading the InnerException property - it should tell you what the exception in fact is. Also, have a look at the call stack so you know where to look.

    If you don't have much experience with debugging, you can also post both of these things here in the forums and we'll do our best to help.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  5. #5

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    25

    Re: filewather

    Quote Originally Posted by obi1kenobi View Post
    Hmmm... That's the nastiest type of exception in my opinion. Try reading the InnerException property - it should tell you what the exception in fact is. Also, have a look at the call stack so you know where to look.

    If you don't have much experience with debugging, you can also post both of these things here in the forums and we'll do our best to help.
    please can you just help me with the error? have bad experince in debug

  6. #6
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: filewather

    Of course, but we need the data I asked you to look at - that's what I said in my previous post. Post the Stack trace and the InnerException. Also, if you are doing any work in background threads, double check them for any obvious errors, since that's usually how invocation exceptions occur.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  7. #7

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    25

    Re: filewather

    Quote Originally Posted by obi1kenobi View Post
    Of course, but we need the data I asked you to look at - that's what I said in my previous post. Post the Stack trace and the InnerException. Also, if you are doing any work in background threads, double check them for any obvious errors, since that's usually how invocation exceptions occur.
    sry but i dont know where to find the data?
    never heird about stack?

  8. #8
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: filewather

    Where did you see the message "exception has been thrown by the target of an invocation"? A small window popped up in Visual Studio, showing that message and highlighting a line of code in yellow, right? On that small window, there is a link saying something like "Show details for this exception". Click on it and a new window will open. Scroll to the Stack trace field and copy the text inside - there should be quite a bit of text so make sure you copy it all. Then scroll to the InnerException field and copy the exception name, message and its Stack trace. Then paste all of this here along with the highlighted line and its neighboring code.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  9. #9

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    25

    Re: filewather

    Quote Originally Posted by obi1kenobi View Post
    Where did you see the message "exception has been thrown by the target of an invocation"? A small window popped up in Visual Studio, showing that message and highlighting a line of code in yellow, right? On that small window, there is a link saying something like "Show details for this exception". Click on it and a new window will open. Scroll to the Stack trace field and copy the text inside - there should be quite a bit of text so make sure you copy it all. Then scroll to the InnerException field and copy the exception name, message and its Stack trace. Then paste all of this here along with the highlighted line and its neighboring code.
    is it this?
    Code:
      at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       at System.RuntimeMethodHandle.InvokeMethodFast(Object target, Object[] arguments, Signature sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
       at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
       at System.Delegate.DynamicInvokeImpl(Object[] args)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
       at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       at System.Windows.Forms.ContainerControl.WndProc(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

  10. #10
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: filewather

    Yeah, that's the stack trace of the exception. Now try the same for the InnerException, just make sure to copy the Message and exception type too.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  11. #11

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    25

    Re: filewather

    Quote Originally Posted by obi1kenobi View Post
    Yeah, that's the stack trace of the exception. Now try the same for the InnerException, just make sure to copy the Message and exception type too.
    Code:
    {"The process cannot access the file 'C:\\Users\\Martin\\Desktop\\New Text Document (2).txt' because it is being used by another process."}
    message
    Code:
    Exception has been thrown by the target of an invocation.

  12. #12
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: filewather

    Are you opening the C:\Users\Martin\Desktop\New Text Document (2).txt file anywhere in your code? Possibly forgeting to close it after you've used it? Post the method you have written to access and use this file.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  13. #13

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    25

    Re: filewather

    Quote Originally Posted by obi1kenobi View Post
    Are you opening the C:\Users\Martin\Desktop\New Text Document (2).txt file anywhere in your code? Possibly forgeting to close it after you've used it? Post the method you have written to access and use this file.
    its because i have set the filewatcher to watch desktop and then check hash if a new file is created?

  14. #14

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    25

    Re: filewather

    Quote Originally Posted by obi1kenobi View Post
    Are you opening the C:\Users\Martin\Desktop\New Text Document (2).txt file anywhere in your code? Possibly forgeting to close it after you've used it? Post the method you have written to access and use this file.
    its because i set the filewatcher to watch the desktop when a file is created it reads the hash

  15. #15
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: filewather

    Possibly yes, depending on what you do. Find all FileStream objects you create and double-check that you are closing all of them with the Close method. If that doesn't work, post all code that uses FileStream objects and I'll try to help.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  16. #16
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: filewather

    Quote Originally Posted by mgcmartin View Post
    its because i set the filewatcher to watch the desktop when a file is created it reads the hash
    Check if it closes the FileStream after it reads the hash.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  17. #17

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    25

    Re: filewather

    Quote Originally Posted by obi1kenobi View Post
    Check if it closes the FileStream after it reads the hash.
    it dosen't work=/

  18. #18
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: filewather

    What do you mean, it doesn't work? What doesn't work? Read my previous two posts again.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  19. #19

    Thread Starter
    Junior Member
    Join Date
    May 2010
    Posts
    25

    Re: filewather

    Quote Originally Posted by obi1kenobi View Post
    What do you mean, it doesn't work? What doesn't work? Read my previous two posts again.
    its still the same=/ when i create a new textfile its work but i create a new again it gives the error

  20. #20
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    Re: filewather

    Oh come on, did you even read my posts?

    Quote Originally Posted by obi1kenobi View Post
    Possibly yes, depending on what you do. Find all FileStream objects you create and double-check that you are closing all of them with the Close method. If that doesn't work, post all code that uses FileStream objects and I'll try to help.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

Tags for this Thread

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