Re: Creating a self deleting executable using .NET

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
.

.



Relevant Pages

  • Re: CreateFile() and FILE_FLAG_WRITE_THROUGH
    ... BUT this will not work UNLESS you specify the ... FILE_FLAG_NO_BUFFERING flag, ... I have now proved to myself that file metadata is not written ... disk structure to be consistent, this incurs a cost and that's why the ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Letting a user select a color for a control
    ... CC_FULLOPEN flag instead of the CC_SOLIDCOLOR flag in order to do this. ... is there any way to specify where on the screen the Color Dialog ... > specify an initial color selection. ... >> something before calling aDialogColor. ...
    (microsoft.public.access.modulesdaovba)
  • Re: reply-to
    ... but the reply to address appends the servers ... I've figured out that it deals with a sender envelope issue, ... It may be that if you specify the -f option, ... The -r flag is described as an alternate and obsolete form of the ...
    (comp.lang.php)
  • Re: date from yesterday
    ... | | versions of date that uses the -v flag. ... | | adjust units, but without the -/+ preceding the unit it gives the ... Users in countries that didn't switch from the Julian ... Fully functional demos available by email request (specify OS). ...
    (comp.unix.sco.misc)
  • Re: Problem with fopen, linux, and RS-232, possibly
    ... Are those pins you specified the DCD signal? ... you may want the O_NOCTTY flag to be present. ... Try writing the program using openinstead, and specify the exact ... may be unaware of its "readiness", then any opencall may not block. ...
    (comp.lang.c)