Windows: remove EXE after execution

I am working on an application in C # that does the following:

  • Burn EXE to disk
  • Run exe through Process.Start()

Now I am trying to make sure that the EXE will be deleted after it is closed. The easiest way to do this is to set the option FileOptions.DeleteOnClosewhen creating the EXE using File.Create.

However, this means that EXE cannot be executed during use. When the file descriptor is closed, the EXE is immediately deleted before it can be executed.

Is there a way to keep the "weak link" to the EXE in my application, which does not block the file and allows it to execute? Alternatively, is there a way to unlock an EXE to execute with an open file descriptor? Are there any other obvious solutions that I am missing?

ACKNOWLEDGMENT A: I am aware of other methods for deleting used files that will ultimately delete the file (for example, upon reboot). However, I’m looking for a way to delete a file immediately after its launch, which is processed by the OS (for example, when you run a package that first executes the file and then deletes it, the file will remain on disk if the batch job is completed).

CLARIFICATION B: To explain the big picture: the application receives and decrypts the executable file. After decryption, the file must be executed. However, I want the decrypted version of the EXE not to remain on the disk. Ideally, I also want users to not copy the decrypted EXE. However, since the decryption application works as the same user, this will not be possible to achieve in a truly secure way, since both have the same privileges on the system.

+4
source share
2 answers

You can use Process.WaitForExit :

var process = Process.Start(processPath);
process.WaitForExit();

// File.Delete(processPath); // Not strong enough (thanks to Binary Worrier)
DeleteOrDie(processPath);    // Will attempts anything to delete the file.

But this makes it possible to copy exe from the place where you wrote it.

.

exe - CLR, Assembly.Load:

// read the file and put data in bin
...
Assembly a = Assembly.Load(bin);
MethodInfo method = a.EntryPoint;
if (method == null) throw new NoEntryPointException();
object o = a.CreateInstance(method.Name);
method.Invoke(o, null);

.

/ exe , Nebbetts Shuttle, C/++ #.

, Microsoft ( ), , #. , , .

+8

, , , , :
( Console Application )

[1:] :

/// <summary>
/// Check application is running by the name of its process ("processName").
/// </summary>
static bool IsProcessOpen(string processName)
{
    foreach (Processing.Process clsProcess in Processing.Process.GetProcesses())
    {
        if (clsProcess.ProcessName.ToUpper().Contains(processName.ToUpper()))
                return true;
    }
    return false;
}

[2:] :

static bool IamRunning = true; 
static int checkDuration = 1;     // in seconds

[3:] Thread :

Thread t = new Thread(delegate() {
    DateTime lastCheck = DateTime.MinValue;
    while (IamRunning)
    {
        var now = DateTime.Now;
        int dd = (now.Hour - lastCheck.Hour) * 3600 + (now.Minute - lastCheck.Minute) * 60 + now.Second - lastCheck.Second;
        if (dd >= checkDuration)
            if (!IsProcessOpen("ProcessName"))
            {
                delApplication();  // You have a function to delete ...
                break;
            }
    }
});
t.SetApartmentState(ApartmentState.STA);
t.Start();

[4:] :

while (t.ThreadState == ThreadState.Running)
{
    // just wait.
}

. Console Application 50% .

+2

All Articles