Re: Creating a self deleting executable using .NET
- From: PeterW <PeterW@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 23 Nov 2009 14:06:06 -0800
Hi Peter,
Thanks for your reply. You are indeed right. In the code that I posted only
the FILE_SHARE_READ is specified but I also tried it combined with
FILE_SHARE_DELETE which does not work either.
I found an article on the web explaining that the second process that
attempts to open the file also needs to open it with the FILE_SHARE_READ +
FILE_SHARE_DELETE flag set. If this is not the case then the second process
will exclusively open the file and effectively block the first process from
reading the file, this is not allowed and therefore attempting to do so
generates an error. I assume that this is the problem I'm dealing with.
If you open a filestream it's easy to specify the FILE_SHARE_READ +
FILE_SHARE_DELETE flags, but when you try to start up a new executable from
within the code you can't specify these flags. Starting an executable will by
default try to generate an exclusive lock. That's at least what I think is
happening. I need to find a way around this problem.
Yesterday I was doing some further research and I found this article on the
web: http://www.codeproject.com/KB/threads/HowToDeleteCurrentProcess.aspx
It explains a solution for the self delete problem that is very simple and
straightforward. Just add two lines of code:
Start("cmd.exe", "/C choice /C Y /N /D Y /T 3 & Del " +
System.Reflection.Assembly.GetExecutingAssembly.GetModules()(0).FullyQualifiedName)
Application.Exit()
This works very well on my desktop. Unfortunately it does not work on a
Windows Mobile 5.0 device because windows mobile does not have a cmd.exe.
I will have to dig a bit further for a solution.
"Peter Duniho" wrote:
PeterW wrote:.
I had hoped the "FileShare.Read" in the following line of code would do the
trick:
Dim objWriteFileStream As New FileStream("F:\SelfDeleteFromResource.exe",
FileMode.CreateNew, FileAccess.Write, FileShare.Read, 1024,
FileOptions.DeleteOnClose)
Why? The page you referenced specifically says the file you write "must
also have the FILE_SHARE_DELETE flag specified". Why would you think
that FileShare.Read would accomplish that, as opposed to (for example)
the FileShare.Delete flag?
According to the documentation this flag controls if other filestreams can
access the file or not.
Actually, the documentation is more specific: it says that flag "allows
subsequent opening of the file for reading". Not any access; it
specifically controls read sharing.
Also, the page you referenced clearly explains that you need the
FILE_SHARE_DELETE flag, because the file is originally opened with the
DELETE_ON_CLOSE flag (FileOptions.DeleteOnClose in .NET).
It seems to me you want (in addition to other sharing flags that may be
necessary) the equivalent of the FILE_SHARE_DELETE flag, which I believe
is the FileShare.Delete flag.
FileShare.Read means that other filestreams can open
the file for read while the current process still has the file open.
Aparently this is limited to filestream objects only because it does not work
when you try to start an executable. [...]
I disagree with your conclusion. It's likely your code would work fine
if you hadn't also specified the FileOptions.DeleteOnClose, and given
that you are, you need to specify the proper sharing flag to allow that
to happen.
Pete
.
- References:
- Creating a self deleting executable using .NET
- From: PeterW
- Re: Creating a self deleting executable using .NET
- From: Peter Duniho
- Re: Creating a self deleting executable using .NET
- From: PeterW
- Re: Creating a self deleting executable using .NET
- From: Peter Duniho
- Re: Creating a self deleting executable using .NET
- From: PeterW
- Re: Creating a self deleting executable using .NET
- From: Peter Duniho
- Creating a self deleting executable using .NET
- Prev by Date: Re: General SOA question
- Next by Date: Re: Multiple interface implementation
- Previous by thread: Re: Creating a self deleting executable using .NET
- Next by thread: RE: You are correct and why must I repost stupid server?
- Index(es):
Relevant Pages
|