C# close handle to file


















Visual C. Sign in to vote. What I have is a windows form that I use to send an email with an attachment. I pick the file and copy it to a new folder and then I send the message along with the file. So How would I close the file handle on a file I just copied over. PHP, Mysql. Wednesday, October 27, PM. Hello again, A simpler method would be to add a finally clause to your try catch from send, and there check if mail has attachments and if it has call Dispose on each attachment and them delete the file there.

Text; mail. Name ; mail. SmtpServer ; if Properties. LoginName, Properties. Send mail ; MessageBox. Show "Message Sent! Show "Error, Sending Message. Attachments[0]; attachment. Dispose ; File. Delete file. SetTextBox1 "" ; this. SetButton1 "Send" ; this. SetTXTMessage "" ; this. Thursday, October 28, AM. You can declare the function like this: [DllImport "kernel Well I use a browse dialog to choose the file. Copy source, target ; then I send the email with that file in "target" after the email gets sent.

Then I try and delete. Just says it being used and I have one idea. Doing so will result in a sharing violation. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Please rate your experience Yes No. If the function fails, the return value is zero. To get extended error information, call GetLastError. If the application is running under a debugger, the function will throw an exception if it receives either a handle value that is not valid or a pseudo-handle value.

This can happen if you close a handle twice, or if you call CloseHandle on a handle returned by the FindFirstFile function instead of calling the FindClose function.

Generally, an application should call CloseHandle once for each handle it opens. In this case, the application should close the handle. If a handle is transacted, all handles bound to a transaction should be closed before the transaction is committed.

For more information about transacted objects, see Working With Transactions. Closing a thread handle does not terminate the associated thread or remove the thread object. Closing a process handle does not terminate the associated process or remove the process object.

To remove a thread object, you must terminate the thread, then close all handles to the thread. For more information, see Terminating a Thread.



0コメント

  • 1000 / 1000