|
-
Oct 3rd, 2010, 05:13 AM
#1
Thread Starter
Junior Member
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
-
Oct 3rd, 2010, 05:51 AM
#2
Frenzied Member
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 
-
Oct 3rd, 2010, 06:00 AM
#3
Thread Starter
Junior Member
Re: filewather
 Originally Posted by obi1kenobi
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.
-
Oct 3rd, 2010, 06:12 AM
#4
Frenzied Member
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 
-
Oct 3rd, 2010, 06:37 AM
#5
Thread Starter
Junior Member
Re: filewather
 Originally Posted by obi1kenobi
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
-
Oct 3rd, 2010, 06:48 AM
#6
Frenzied Member
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 
-
Oct 3rd, 2010, 07:31 AM
#7
Thread Starter
Junior Member
Re: filewather
 Originally Posted by obi1kenobi
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?
-
Oct 3rd, 2010, 08:15 AM
#8
Frenzied Member
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 
-
Oct 3rd, 2010, 08:24 AM
#9
Thread Starter
Junior Member
Re: filewather
 Originally Posted by obi1kenobi
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()
-
Oct 3rd, 2010, 08:27 AM
#10
Frenzied Member
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 
-
Oct 3rd, 2010, 08:47 AM
#11
Thread Starter
Junior Member
Re: filewather
 Originally Posted by obi1kenobi
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.
-
Oct 3rd, 2010, 08:57 AM
#12
Frenzied Member
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 
-
Oct 3rd, 2010, 09:12 AM
#13
Thread Starter
Junior Member
Re: filewather
 Originally Posted by obi1kenobi
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?
-
Oct 3rd, 2010, 10:01 AM
#14
Thread Starter
Junior Member
Re: filewather
 Originally Posted by obi1kenobi
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
-
Oct 3rd, 2010, 10:04 AM
#15
Frenzied Member
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 
-
Oct 3rd, 2010, 10:05 AM
#16
Frenzied Member
Re: filewather
 Originally Posted by mgcmartin
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 
-
Oct 3rd, 2010, 10:18 AM
#17
Thread Starter
Junior Member
Re: filewather
 Originally Posted by obi1kenobi
Check if it closes the FileStream after it reads the hash.
it dosen't work=/
-
Oct 3rd, 2010, 10:46 AM
#18
Frenzied Member
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 
-
Oct 3rd, 2010, 10:50 AM
#19
Thread Starter
Junior Member
Re: filewather
 Originally Posted by obi1kenobi
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
-
Oct 3rd, 2010, 10:59 AM
#20
Frenzied Member
Re: filewather
Oh come on, did you even read my posts?
 Originally Posted by obi1kenobi
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|