简体   繁体   中英

How to remove file if application is not running?

Not sure whether this is possible, but I'm creating a file encoding applcation. When a file is decoded, it is saved temporarily in a temp directory, after which it can be opened regularly. However, I actually need to be certain the file is removed as soon as the application that has opened it, has closed it (eg has shut down). Otherwise, the decoded (secret) file is just hanging in the temp directory without supervision.

What's more, even when my application itself has been shut down for any reason, I'd like to pass this task on to Windows, if possible. So say the user decodes a file and opens it and then my application is shut down (either normally or abnormally), the decoded file in the temp directory should still be removed as soon as it's not used anymore.

How would I go about this? I've seen tips like FileSystemWatcher and a trivial 'check every second' idea, but if my application is not alive at the moment the decoded file is closed, I'd still like to have the file removed. So I guess I'd need to pass this responbility to Windows, but I'm not sure if that's possible and if so, how.

So how do I remove a file as soon as it's closed if my application isn't running at that particular moment?

Doing this may work:

  1. In the process that creates the file, create it with FileOptions.DeleteOnClose, and with FileShare.ReadWrite (or FileShare.Read if only read access is required from other processes). You may also need FileShare.Delete.
  2. DO NOT let the file close in the main application that created it until the application exits.
  3. In other processes that consume the temporary file, open it with the same file options as the original.

This way, when the last process that has the file open closes, the file will be deleted.

UPDATE:

As noted in the comments, there doesn't seem to be a way in the .NET API to specify both the FIleShare options and the FileOptions.DeleteOnClose. It is possible using straight Win32. I have copied a sample that I tested below. There are 2 programs, one that creates the file, another that consumes it. The only notable difference between the 2 is that the consumer opens the file with OPEN_EXISTING.

Creator

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE fh = CreateFile(
        L"yourFilePath\\tempFile.dat",
        GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_SHARE_DELETE,
        NULL,
        CREATE_NEW,
        FILE_FLAG_DELETE_ON_CLOSE,
        NULL);
    if(fh==INVALID_HANDLE_VALUE)
    {
        std::cerr << "Failed to create file. Error code = " << GetLastError() << std::endl;
        return 1;
    }

    std::cout<< "Hit enter to close.";
    std::string inp;
    std::getline(std::cin,inp);

    CloseHandle(fh);


    return 0;
}

Consumer

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{
    HANDLE fh = CreateFile(
        L"yourFilePath\\tempFile.dat",
        GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_WRITE|FILE_SHARE_READ|FILE_SHARE_DELETE,
        NULL,
        OPEN_EXISTING,
        FILE_FLAG_DELETE_ON_CLOSE,
        NULL);
    if(fh==INVALID_HANDLE_VALUE)
    {
        std::cerr << "Failed to create file. Error code = " << GetLastError() << std::endl;
        return 1;
    }

    DWORD written;
    if(!WriteFile(fh,"Test",4,&written,NULL))
    {
        std::cerr << "Failed to write data to file. Error code = " << GetLastError() << std::endl;
        return 1;
    }


    std::cout<< "Hit enter to close.";
    std::string inp;
    std::getline(std::cin,inp);

    CloseHandle(fh);


    return 0;
}

使用FileOptions.DeleteOnClose。

Things like FileOptions.DeleteOnClose won't help if your media becomes unavailable or the machine gets shut down before the delete occurs. To me this looks very much like an exogenous condition .

Can you stream the decoding to a memory stream rather than to disk and take the whole problem away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM